import { defineStore } from 'pinia';
import { ref } from 'vue';
import { EAuthEmailType, EAuthStep, IAuthTexts } from '@/modules/auth/type';
import { uiKitI18n } from '@/configs';
import { useUIModalStore } from '@/UI-kit/store';

export const useAuthStore = defineStore('auth', () => {
    const { t } = uiKitI18n.global;

    const { closeModal } = useUIModalStore();

    const authModalId = ref<null | number>(null);
    const authStep = ref<EAuthStep>(EAuthStep.input);
    const authEmailType = ref<EAuthEmailType>(EAuthEmailType.link);
    const isLoadingAuthUserWithEmail = ref<boolean>(false);
    const userEmail = ref<string>('');

    const emailConfirmationInterval = ref();
    const countInterval = ref(0);

    const defaultTranslates = (): IAuthTexts => {
        return {
            title: t('moduleAuth.register'),
            input: {
                description: t('moduleAuth.input.description'),
                label: t('moduleAuth.input.email'),
                btn: t('moduleAuth.input.btn'),
                divider: t('moduleAuth.input.divider'),
                error: t('helpers.validation.error'),
            },
            confirm: {
                link: {
                    label: t('moduleAuth.login.email'),
                    btnToMail: t('moduleAuth.login.email_btn_next'),
                },
                code: {
                    description: t('moduleAuth.login.description'),
                    text: t('moduleAuth.confirm.code.text'),
                    error: t('moduleAuth.confirm.code.error'),
                    btn: {
                        ok: t('moduleAuth.confirm.code.btn.ok'),
                        cancel: t('moduleAuth.confirm.code.btn.cancel'),
                    },
                },
            },
        };
    };
    const translates = ref<IAuthTexts>({} as IAuthTexts);

    const modalClose = () => {
        if (authModalId.value) {
            rmInterval();
            closeModal(authModalId.value);
        }
        authModalId.value = null;
    };

    const rmInterval = () => {
        if (emailConfirmationInterval.value) {
            countInterval.value = 0;
            clearInterval(emailConfirmationInterval.value);
        }
    };

    const destroyAuthState = () => {
        translates.value = defaultTranslates();
        countInterval.value = 0;
        isLoadingAuthUserWithEmail.value = false;
        authStep.value = EAuthStep.input;
        authEmailType.value = EAuthEmailType.link;
        userEmail.value = '';
        modalClose();
        rmInterval();
    };

    const editSentEmail = () => {
        authStep.value = EAuthStep.input;
        isLoadingAuthUserWithEmail.value = false;
        rmInterval();
    };

    return {
        authModalId,
        translates,
        authStep,
        userEmail,

        emailConfirmationInterval,
        authEmailType,
        isLoadingAuthUserWithEmail,
        editSentEmail,

        defaultTranslates,
        modalClose,
        destroyAuthState,
        rmInterval,
    };
});
