import { lens } from '@dhmk/zustand-lens';
import { SetStateAction } from 'react';
import { BannerInfo } from './types';

export interface BannerInfoState {
  bannerInfo: BannerInfo;
  setBannerInfo: (update: SetStateAction<Partial<BannerInfo>>) => void;
};

export interface CommonState {
  serviceCodes: Array<any>;
  setServiceCodes: (update: SetStateAction<Array<any>>) => void;
  creditCardList: Array<any>;
  setCreditCardList: (update: SetStateAction<Array<any>>) => void;
  bankList: Array<any>;
  setBankList: (update: SetStateAction<Array<any>>) => void;
  virtualBankList: Array<any>;
  setVirtualBankList: (update: SetStateAction<Array<any>>) => void;
  appNotificationCategories: Array<any>;
  setAppNotificationCategories: (update: SetStateAction<Array<any>>) => void;
};

const initialBannerInfoState = {         
  bannerInfo: {} as BannerInfo,
} as BannerInfoState;

const initialCommonState = {
  serviceCodes: [] as Array<any>,
  creditCardList: [] as Array<any>,
  bankList: [] as Array<any>,
  virtualBankList: [] as Array<any>,
  appNotificationCategories: [] as Array<any>
} as CommonState;

export const createBannerInfoStore = lens<BannerInfoState>((set, get) => ({
  ...initialBannerInfoState,
  setBannerInfo: (update) => {
    set((state: BannerInfoState) => {
      const newBannerInfo = (typeof update === 'function')
        ? update(state.bannerInfo): update;
      return {
        ...state,
        bannerInfo: {
          ...state.bannerInfo,
          ...newBannerInfo
        },
      };
    });
  },
  
}));

export const createCommonStore = lens<CommonState>((set, get) => ({
  ...initialCommonState,
  setServiceCodes: (update) => {
    set((state: CommonState) => {
      const newServiceCodes = (typeof update === 'function')
        ? update(state.serviceCodes): update;
        return {
          ...state,
          serviceCodes: [
            ...newServiceCodes
          ]
        };
    });
  },
  setCreditCardList: (update) => {
    set((state: CommonState) => {
      const newCreditCardList = (typeof update === 'function')
        ? update(state.creditCardList): update;
        return {
          ...state,
          creditCardList: [
            ...newCreditCardList
          ]
        };
    });
  },
  setBankList: (update) => {
    set((state: CommonState) => {
      const newBankList = (typeof update === 'function')
        ? update(state.bankList): update;
        return {
          ...state,
          bankList: [
            ...newBankList
          ]
        };
    });
  },
  setVirtualBankList: (update) => {
    set((state: CommonState) => {
      const newVirtualBankList = (typeof update === 'function')
        ? update(state.virtualBankList): update;
        return {
          ...state,
          virtualBankList: [
            ...newVirtualBankList
          ]
        };
    });
  },
  setAppNotificationCategories: (update) => {
    set((state: CommonState) => {
      const newAppNotificationCategories = (typeof update === 'function')
        ? update(state.appNotificationCategories): update;
        return {
          ...state,
          appNotificationCategories: [
            ...newAppNotificationCategories
          ]
        };
    });
  },
}));
