import { calculateInsuranceWage } from "../insuranceWage";
import { calculateSalaryTax } from "../salaryTax";
import {
    calculateSocialInsurance,
    calculateSocialInsuranceEmployerShare,
} from "../socialInsurance";
import type { VariantCalculations } from "@/types/salary";
import { LawYear } from "@/types/salary";
import type { CalculateNetSalaryParams } from "./types";

const calculateNetSalary = ({
    grossSalary,
    insuranceWage,
    bonus = 0,
    deduction = 0,
    isInsured = true,
    lawYear = LawYear.TWENTY_FOUR,
}: CalculateNetSalaryParams): [number, VariantCalculations] => {
    grossSalary += bonus;
    grossSalary -= deduction;

    // TODO: check if the input insurance wage is allowed
    insuranceWage = isInsured
        ? insuranceWage ?? calculateInsuranceWage(grossSalary, lawYear)
        : 0;

    const socialInsuranceWorkerShare = calculateSocialInsurance(insuranceWage);

    const socialInsuranceEmployerShare =
        calculateSocialInsuranceEmployerShare(insuranceWage);

    const martyrsFamiliesFunds = grossSalary * 0.0005;

    const salaryTax = calculateSalaryTax({
        grossSalary,
        socialInsuranceWorkerShare,
        lawYear,
    });

    const totalDeduction =
        socialInsuranceWorkerShare + martyrsFamiliesFunds + salaryTax;

    const netSalary = grossSalary - totalDeduction;

    return [
        netSalary,
        {
            insuranceWage,
            socialInsuranceWorkerShare,
            salaryTax,
            martyrsFamiliesFunds,
            socialInsuranceEmployerShare,
            totalDeduction,
        },
    ];
};

export { calculateNetSalary };
export type { CalculateNetSalaryParams };
