import { BankCartInterface, BanksInterface } from "../types";

export const formatCartNumber = (num: string | number) => {
    return num?.toString().replace(/\B(?=(\d{4})+(?!\d))/g, `    `);
}

export let bankCarts: BankCartInterface[] | [] | any = []

const banks: BanksInterface[] | [] = [
    {
        name: "ansar",
        name_farsi: "انصار",
        icon: <i className="ibl32 ibl-ansar" />,
        iban_nbc: "063",
        pan_iin: [
            "627381"
        ]
    },
    {
        name: "ayandeh",
        name_farsi: "آینده",
        icon: <i className="ibl32 ibl-ba" />,
        iban_nbc: "062",
        pan_iin: [
            "636214"
        ]
    },
    {
        name: "dey",
        name_farsi: "دی",
        icon: <i className="ibl32 ibl-day" />,
        iban_nbc: "066",
        pan_iin: [
            "502938"
        ]
    },
    {
        name: "eghtesad_novin",
        name_farsi: "اقتصاد نوین",
        icon: <i className="ibl32 ibl-en" />,
        iban_nbc: "055",
        pan_iin: [
            "627412",
            "627884"
        ]
    },
    {
        name: "gardeshgari",
        name_farsi: "گردشگری",
        icon: <i className="ibl32 ibl-tourism" />,
        iban_nbc: "064",
        pan_iin: [
            "505416"
        ]
    },
    {
        name: "ghavamin",
        name_farsi: "قوامین",
        icon: <i className="ibl32 ibl-ghbi" />,
        iban_nbc: "052",
        pan_iin: [
            "639599"
        ]
    },
    {
        name: "hekmat",
        name_farsi: "حکمت",
        icon: <i className="ibl32 ibl-hi" />,
        iban_nbc: "065",
        pan_iin: [
            "636949"
        ]
    },
    {
        name: "iran_zamin",
        name_farsi: "ایران‌زمین",
        icon: <i className="ibl32 ibl-iz" />,
        iban_nbc: "069",
        pan_iin: [
            "505785",
        ]
    },
    {
        name: "karafarin",
        name_farsi: "کارآفرین",
        icon: <i className="ibl32 ibl-kar" />,
        iban_nbc: "053",
        pan_iin: [
            "502910",
            "627488"
        ]
    },
    {
        name: "keshavarzi",
        name_farsi: "کشاورزی",
        icon: <i className="ibl32 ibl-bki" />,
        iban_nbc: "016",
        pan_iin: [
            "603770",
            "639217"
        ]
    },
    {
        name: "khavar_mianeh",
        name_farsi: "خاورمیانه",
        icon: <i className="ibl32 ibl-me" />,
        iban_nbc: "078",
        pan_iin: [
            "505809"
        ]
    },
    {
        name: "maskan",
        name_farsi: "مسکن",
        icon: <i className="ibl32 ibl-maskan" />,
        iban_nbc: "014",
        pan_iin: [
            "628023"
        ]
    },
    {
        name: "mehr_iran",
        name_farsi: "مهر ایران",
        icon: <i className="ibl32 ibl-miran" />,
        iban_nbc: "060",
        pan_iin: [
            "606373"
        ]
    },
    {
        name: "melall",
        name_farsi: "ملل",
        icon: <i className="ibl32 ibl-melal" />,
        iban_nbc: "075",
        pan_iin: [
            "606256"
        ]
    },
    {
        name: "mellat",
        name_farsi: "ملت",
        icon: <i className="ibl32 ibl-mellat" />,
        iban_nbc: "012",
        pan_iin: [
            "610433",
            "991975"
        ]
    },
    {
        name: "melli",
        name_farsi: "ملی",
        icon: <i className="ibl32 ibl-bmi" />,
        iban_nbc: "017",
        pan_iin: [
            "603799",
            "589905",
            "170019"
        ]
    },
    {
        name: "parsian",
        name_farsi: "پارسیان",
        icon: <i className="ibl32 ibl-parsian" />,
        iban_nbc: "054",
        pan_iin: [
            "622106",
            "639194"
        ]
    },
    {
        name: "pasargad",
        name_farsi: "پاسارگاد",
        icon: <i className="ibl32 ibl-bpi" />,
        iban_nbc: "057",
        pan_iin: [
            "502229",
            "639347"
        ]
    },
    {
        name: "postbank",
        name_farsi: "پست‌بانک",
        icon: <i className="ibl32 ibl-post" />,
        iban_nbc: "021",
        pan_iin: [
            "627760"
        ]
    },
    {
        name: "refah",
        name_farsi: "رفاه",
        icon: <i className="ibl32 ibl-rb2" />,
        iban_nbc: "013",
        pan_iin: [
            "589463"
        ]
    },
    {
        name: "resalat",
        name_farsi: "رسالت",
        icon: <i className="ibl32 ibl-resalat" />,
        iban_nbc: "070",
        pan_iin: [
            "504172"
        ]
    },
    {
        name: "saderat",
        name_farsi: "صادرات",
        icon: <i className="ibl32 ibl-bsi" />,
        iban_nbc: "019",
        pan_iin: [
            "603769"
        ]
    },
    {
        name: "saman",
        name_farsi: "سامان",
        icon: <i className="ibl32 ibl-sb" />,
        iban_nbc: "056",
        pan_iin: [
            "621986"
        ]
    },
    {
        name: "sanat_madan",
        name_farsi: "صنعت و معدن",
        icon: <i className="ibl32 ibl-bim" />,
        iban_nbc: "011",
        pan_iin: [
            "627961"
        ]
    },
    {
        name: "sarmayeh",
        name_farsi: "سرمایه",
        icon: <i className="ibl32 ibl-sarmayeh" />,
        iban_nbc: "058",
        pan_iin: [
            "639607"
        ]
    },
    {
        name: "sepah",
        name_farsi: "سپه",
        icon: <i className="ibl32 ibl-sepah" />,
        iban_nbc: "015",
        pan_iin: [
            "589210"
        ]
    },
    {
        name: "shahr",
        name_farsi: "شهر",
        icon: <i className="ibl32 ibl-shahr" />,
        iban_nbc: "061",
        pan_iin: [
            "502806",
            "504706"
        ]
    },
    {
        name: "sina",
        name_farsi: "سینا",
        icon: <i className="ibl32 ibl-sina" />,
        iban_nbc: "059",
        pan_iin: [
            "639346"
        ]
    },
    {
        name: "tejarat",
        name_farsi: "تجارت",
        icon: <i className="ibl32 ibl-tejarat" />,
        iban_nbc: "018",
        pan_iin: [
            "585983",
            "627353"
        ]
    },
    {
        name: "tosee_saderat",
        name_farsi: "توسعه صادرات",
        icon: <i className="ibl32 ibl-edbi" />,
        iban_nbc: "020",
        pan_iin: [
            "207177",
            "627648"
        ]
    },
    {
        name: "tosee_taavon",
        name_farsi: "توسعه تعاون",
        icon: <i className="ibl32 ibl-tt" />,
        iban_nbc: "022",
        pan_iin: [
            "502908"
        ]
    }
]

let cache: any = {};

export const findBankByPAN = (cardNumber: string | number) => {
    cardNumber = String(cardNumber);
    if (!/^[\d*]+$/.test(cardNumber) || cardNumber.length < 6) return {};
    const iin: string = cardNumber.slice(0, 6);

    if (cache[iin]) {
        return cache[iin];
    } else {
        const bank: any = banks?.find((bank: any) => bank?.pan_iin.includes(iin))
        if (!bank) return {};
        cache[iin] = { ...bank };
        return bank;
    };
};