import { format } from 'date-fns';
import { AdminContextProvider } from '../../admin-context/providers/admin-context.provider.js';
import { SalariesProvider } from '../providers/salaries.provider.js';
export const insertSalaryRecords = async (_, params, { injector }) => {
    try {
        const { ownerId } = await injector.get(AdminContextProvider).getVerifiedAdminContext();
        const salaryRecords = params.salaryRecords.map(salaryRecord => ({
            ...salaryRecord,
            ownerId,
            employee: salaryRecord.employee ?? null,
            addedVacationDays: salaryRecord.addedVacationDays ?? null,
            baseSalary: salaryRecord.baseSalary ?? null,
            bonus: salaryRecord.bonus ?? null,
            chargeId: salaryRecord.chargeId ?? null,
            compensationsEmployerAmount: salaryRecord.compensationsEmployerAmount ?? null,
            compensationsEmployerPercentage: salaryRecord.compensationsEmployerPercentage ?? null,
            gift: salaryRecord.gift ?? null,
            globalAdditionalHours: salaryRecord.globalAdditionalHours ?? null,
            healthPaymentAmount: salaryRecord.healthPaymentAmount ?? null,
            hourlyRate: salaryRecord.hourlyRate ?? null,
            hours: salaryRecord.hours ?? null,
            jobPercentage: salaryRecord.jobPercentage ?? null,
            month: salaryRecord.month && salaryRecord.month.length !== 7
                ? format(new Date(salaryRecord.month), 'yyyy-MM')
                : salaryRecord.month,
            pensionEmployeeAmount: salaryRecord.pensionEmployeeAmount ?? null,
            pensionEmployeePercentage: salaryRecord.pensionEmployeePercentage ?? null,
            pensionEmployerAmount: salaryRecord.pensionEmployerAmount ?? null,
            pensionEmployerPercentage: salaryRecord.pensionEmployerPercentage ?? null,
            pensionFundId: salaryRecord.pensionFundId ?? null,
            recovery: salaryRecord.recovery ?? null,
            sicknessDaysBalance: salaryRecord.sicknessDaysBalance ?? null,
            socialSecurityAmountEmployee: salaryRecord.socialSecurityAmountEmployee ?? null,
            socialSecurityAmountEmployer: salaryRecord.socialSecurityAmountEmployer ?? null,
            taxAmount: salaryRecord.taxAmount ?? null,
            trainingFundEmployeeAmount: salaryRecord.trainingFundEmployeeAmount ?? null,
            trainingFundEmployeePercentage: salaryRecord.trainingFundEmployeePercentage ?? null,
            trainingFundEmployerAmount: salaryRecord.trainingFundEmployerAmount ?? null,
            trainingFundEmployerPercentage: salaryRecord.trainingFundEmployerPercentage ?? null,
            trainingFundId: salaryRecord.trainingFundId ?? null,
            travelAndSubsistence: salaryRecord.travelAndSubsistence ?? null,
            vacationDaysBalance: salaryRecord.vacationDaysBalance ?? null,
            vacationTakeout: salaryRecord.vacationTakeout ?? null,
            workDays: salaryRecord.workDays ?? null,
            zkufot: salaryRecord.zkufot ?? null,
        }));
        const res = await injector.get(SalariesProvider).insertSalaryRecords({ salaryRecords });
        if (res.length !== salaryRecords.length) {
            throw new Error(`Failed to insert salary records`);
        }
        return {
            salaryRecords: res,
        };
    }
    catch (e) {
        return {
            __typename: 'CommonError',
            message: e?.message ??
                e?.errors.map(e => e.message).toString() ??
                'Unknown error',
        };
    }
};
//# sourceMappingURL=insert-salary.resolver.js.map