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: <img src="/svgs/banks/icons/ansar.svg" alt="ansar" />,
        iban_nbc: "063",
        pan_iin: [
            "627381"
        ]
    },
    {
        name: "ayandeh",
        name_farsi: "آینده",
        icon: <img src="/svgs/banks/icons/ayandeh.svg" alt="ayandeh" />,
        iban_nbc: "062",
        pan_iin: [
            "636214"
        ]
    },
    {
        name: "dey",
        name_farsi: "دی",
        icon: <img src="/svgs/banks/icons/dey.svg" alt="dey" />,
        iban_nbc: "066",
        pan_iin: [
            "502938"
        ]
    },
    {
        name: "eghtesad_novin",
        name_farsi: "اقتصاد نوین",
        icon: <img src="/svgs/banks/icons/eghtesad_novin.svg" alt="eghtesad_novin" />,
        iban_nbc: "055",
        pan_iin: [
            "627412",
            "627884"
        ]
    },
    {
        name: "gardeshgari",
        name_farsi: "گردشگری",
        icon: <img src="/svgs/banks/icons/gardeshgari.svg" alt="gardeshgari" />,
        iban_nbc: "064",
        pan_iin: [
            "505416"
        ]
    },
    {
        name: "ghavamin",
        name_farsi: "قوامین",
        icon: <img src="/svgs/banks/icons/ghavamin.svg" alt="ghavamin" />,
        iban_nbc: "052",
        pan_iin: [
            "639599"
        ]
    },
    {
        name: "hekmat",
        name_farsi: "حکمت",
        icon: <img src="/svgs/banks/icons/hekmat.svg" alt="hekmat" />,
        iban_nbc: "065",
        pan_iin: [
            "636949"
        ]
    },
    {
        name: "iran_zamin",
        name_farsi: "ایران‌زمین",
        icon: <img src="/svgs/banks/icons/iran_zamin.svg" alt="iran_zamin" />,
        iban_nbc: "069",
        pan_iin: [
            "505785",
        ]
    },
    {
        name: "karafarin",
        name_farsi: "کارآفرین",
        icon: <img src="/svgs/banks/icons/karafarin.svg" alt="karafarin" />,
        iban_nbc: "053",
        pan_iin: [
            "502910",
            "627488"
        ]
    },
    {
        name: "keshavarzi",
        name_farsi: "کشاورزی",
        icon: <img src="/svgs/banks/icons/keshavarzi.svg" alt="keshavarzi" />,
        iban_nbc: "016",
        pan_iin: [
            "603770",
            "639217"
        ]
    },
    {
        name: "khavar_mianeh",
        name_farsi: "خاورمیانه",
        icon: <img src="/svgs/banks/icons/khavar-mianeh.svg" alt="khavar mianeh" />,
        iban_nbc: "078",
        pan_iin: [
            "505809"
        ]
    },
    {
        name: "maskan",
        name_farsi: "مسکن",
        icon: <img src="/svgs/banks/icons/maskan.svg" alt="maskan" />,
        iban_nbc: "014",
        pan_iin: [
            "628023"
        ]
    },
    {
        name: "mehr_iran",
        name_farsi: "مهر ایران",
        icon: <img src="/svgs/banks/icons/mehr.svg" alt="mehr" />,
        iban_nbc: "060",
        pan_iin: [
            "606373"
        ]
    },
    {
        name: "melall",
        name_farsi: "ملل",
        icon: <img src="/svgs/banks/icons/melal.svg" alt="melal" />,
        iban_nbc: "075",
        pan_iin: [
            "606256"
        ]
    },
    {
        name: "mellat",
        name_farsi: "ملت",
        icon: <img src="/svgs/banks/icons/melat.svg" alt="melat" />,
        iban_nbc: "012",
        pan_iin: [
            "610433",
            "991975"
        ]
    },
    {
        name: "melli",
        name_farsi: "ملی",
        icon: <img src="/svgs/banks/icons/meli.svg" alt="meli" />,
        iban_nbc: "017",
        pan_iin: [
            "603799",
            "589905",
            "170019"
        ]
    },
    {
        name: "parsian",
        name_farsi: "پارسیان",
        icon: <img src="/svgs/banks/icons/parsian.svg" alt="parsian" />,
        iban_nbc: "054",
        pan_iin: [
            "622106",
            "639194"
        ]
    },
    {
        name: "pasargad",
        name_farsi: "پاسارگاد",
        icon: <img src="/svgs/banks/icons/pasargad.svg" alt="pasargad" />,
        iban_nbc: "057",
        pan_iin: [
            "502229",
            "639347"
        ]
    },
    {
        name: "postbank",
        name_farsi: "پست‌بانک",
        icon: <img src="/svgs/banks/icons/post-bank.svg" alt="post bank" />,
        iban_nbc: "021",
        pan_iin: [
            "627760"
        ]
    },
    {
        name: "refah",
        name_farsi: "رفاه",
        icon: <img src="/svgs/banks/icons/refah.svg" alt="refah" />,
        iban_nbc: "013",
        pan_iin: [
            "589463"
        ]
    },
    {
        name: "resalat",
        name_farsi: "رسالت",
        icon: <img src="/svgs/banks/icons/resalat.svg" alt="resalat" />,
        iban_nbc: "070",
        pan_iin: [
            "504172"
        ]
    },
    {
        name: "saderat",
        name_farsi: "صادرات",
        icon: <img src="/svgs/banks/icons/saderat.svg" alt="saderat" />,
        iban_nbc: "019",
        pan_iin: [
            "603769"
        ]
    },
    {
        name: "saman",
        name_farsi: "سامان",
        icon: <img src="/svgs/banks/icons/saman.svg" alt="saman" />,
        iban_nbc: "056",
        pan_iin: [
            "621986"
        ]
    },
    {
        name: "sanat_madan",
        name_farsi: "صنعت و معدن",
        icon: <img src="/svgs/banks/icons/sanat-madan.svg" alt="sanat madan" />,
        iban_nbc: "011",
        pan_iin: [
            "627961"
        ]
    },
    {
        name: "sarmayeh",
        name_farsi: "سرمایه",
        icon: <img src="/svgs/banks/icons/sarmaye.svg" alt="sarmaye" />,
        iban_nbc: "058",
        pan_iin: [
            "639607"
        ]
    },
    {
        name: "sepah",
        name_farsi: "سپه",
        icon: <img src="/svgs/banks/icons/sepah.svg" alt="sepah" />,
        iban_nbc: "015",
        pan_iin: [
            "589210"
        ]
    },
    {
        name: "shahr",
        name_farsi: "شهر",
        icon: <img src="/svgs/banks/icons/shahr.svg" alt="shahr" />,
        iban_nbc: "061",
        pan_iin: [
            "502806",
            "504706"
        ]
    },
    {
        name: "sina",
        name_farsi: "سینا",
        icon: <img src="/svgs/banks/icons/sina.svg" alt="sina" />,
        iban_nbc: "059",
        pan_iin: [
            "639346"
        ]
    },
    {
        name: "tejarat",
        name_farsi: "تجارت",
        icon: <img src="/svgs/banks/icons/tejarat.svg" alt="tejarat" />,
        iban_nbc: "018",
        pan_iin: [
            "585983",
            "627353"
        ]
    },
    {
        name: "tosee_saderat",
        name_farsi: "توسعه صادرات",
        icon: <img src="/svgs/banks/icons/tsaderat.svg" alt="tosee saderat" />,
        iban_nbc: "020",
        pan_iin: [
            "207177",
            "627648"
        ]
    },
    {
        name: "tosee_taavon",
        name_farsi: "توسعه تعاون",
        icon: <img src="/svgs/banks/icons/taavon.svg" alt="tosee taavon" />,
        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;
    };
};