import {
    periodTypeConstants,
    restatementType,
    dataSourceConstants,
    platforms,
    financialQuarter
} from '@root/constants';
import getPeriodDate from '@utils/periodDate';

export const getPrivateorPublicPeriodType = (selectedValues, isPrivateCompany) => selectedValues.periodType
    ? selectedValues.periodType
    : isPrivateCompany
        ? periodTypeConstants[0]
        : periodTypeConstants[1];

export const getCurrencyObject = (currency, companyMetaData) => {
    const currencyData = companyMetaData.currencies;
    const currencyValue = currencyData?.find(c => c.code === currency);
    if (currencyValue) {
        return {
            value: currencyValue.code,
            label: currencyValue.name + '[' + currencyValue.code + ']'
        };
    }
    return currencyValue;
};


export const selectValue = (val, defaultItem) => {
    const returnVal = val && Object.keys(val).length > 0 ? val : defaultItem;
    return returnVal;
};

export const getPeriodTypeConstants = (isPrivate, extractedData) => {
    let periodType = isPrivate ? periodTypeConstants[0] : periodTypeConstants[1];
    const isExtractedPeriodType = extractedData?.instances?.length && extractedData.instances[0]?.Period;
    if (isExtractedPeriodType) {
        periodType = extractedData.instances[0].Period === 'FY' ? periodTypeConstants[0] : periodTypeConstants[1];
    }
    return periodType;
};

export const getFinancialStatementOptions = financialStatementList =>
    financialStatementList.map((c, index) => ({
        value: c.periodType === 'FY' ? c.fiscalYear.toString() : c.periodDate,
        label: c.periodType === 'FY' ? c.fiscalYear.toString() : c.periodDate,
        isProp: c.restatementType === restatementType.LP,
        currencyCode: c?.currencyIso,
        id: index,
        fiscalQuarter: c.fiscalQuarter,
        fiscalYear: c.fiscalYear,
        periodDate: c.newDate ? c.newDate : c.periodDate,
        periodType: c.periodType,
        restatementType: c.restatementType
    }));

export const getSelectPeriodOptions = selectPeriodList =>
    selectPeriodList.map((c, index) => ({
        label: `FY ${c.fiscalYear}`,
        value: c.fiscalYear,
        currencyCode: c?.currencyIso,
        restatementType: c?.restatementType,
        id: index,
        isProp: c.restatementType === restatementType.LP,
        periodDate: c?.periodDate,
        fiscalQuarter: c?.fiscalQuarter
    }));

export const companyDatesPayload = ({
    csCompanyId, isPropDataSource, isUnlinkCompany, isUnlinkPropCompany, companyId, latestStmtDate = '', ...rest
}) => ({
    ...rest,
    ...(csCompanyId && { csCompanyId }),
    ...(isPropDataSource ? { datasource: platforms.CIQPROP, companyId }
        : ((isUnlinkCompany || isUnlinkPropCompany)
            ? { datasource: platforms.CIQUNLINK, ...(latestStmtDate && { pdfnStartDate: latestStmtDate }) }
            : { datasource: platforms.CIQ, companyId }))
});

const removeExtarctedDataSource = dataSourceConstants.filter(item => item.label !== 'Extracted');

export const getDataSourceOptions = (isPropCompany = false, isUnlinkPropCompany = false, isExtractedData = false
    , isRVLinkedCompany = false) =>
    (isPropCompany && !isUnlinkPropCompany && !isExtractedData)
    ? [dataSourceConstants[1]]
    : (isExtractedData ? (isRVLinkedCompany ? removeExtarctedDataSource : dataSourceConstants)
    : removeExtarctedDataSource);

export const getDataSourceText = (t, isUnlinkCompany = false, isExtractedData = false,
    isRVunLinkedCompany = false, unlinkedSource = false ) => isUnlinkCompany
    ? dataSourceConstants[0].label
    : (isExtractedData
    ? (isRVunLinkedCompany
    ? (unlinkedSource
    ? t('scoringui:companyInput:datasourceExpand')
    : t('scoringui:companyInput:datasourceCapital'))
    : t('scoringui:companyInput:datasourceExtract'))
    : t('scoringui:companyInput:none'));

export const checkExtractedStmt = (dataSource, extractedData) =>
    (dataSource === dataSourceConstants[2].value) && extractedData?.instances?.length;

export const getExtractedStatements = ({ extractedData, selectedValues, isLTMperiodType }) => {
    const statement = extractedData?.instances;
    const extractedStatementList = [];
    statement.forEach(st => {
        if (selectedValues.periodType.label === st.Period) {
            const fiscalQuarter = Number(st.FQ);
            const currencyIso = extractedData?.currencyName.replace(/(^.*\[|\].*$)/g, '');
            const restatementType = 'LC';
            const fiscalYear = Number(st.FY);
            const newDate = getPeriodDate({
                newQtr: fiscalQuarter, periodTp: st.Period, newYr: fiscalYear })?.newStmtDate;
            let periodDate = newDate;

            if (isLTMperiodType) {
                const addedQuarter = financialQuarter.find(item => Number(item.value) === fiscalQuarter);
                periodDate = `${addedQuarter.label} ${fiscalYear}`;
            }

            extractedStatementList.push({
                periodDate,
                currencyIso,
                restatementType,
                fiscalYear,
                fiscalQuarter,
                periodType: st.Period,
                newDate
            });
        }
    });
    return extractedStatementList;
};
