import {
    TimeTrackingCategorizedHourApprovalStep,
    TimeTrackingCategorizedHourApprovalStepForCreation,
} from '../../../business/models/timeTracking/timeTrackingCategorizedHourApprovalStep';
import { TimeTrackingCategorizedHourApprovalStepDAO } from '../../dataAccessObjects/timeTracking/timeTrackingCategorizedHourApprovalStepDAO';
import { timeTrackingCategorizedHourApprovalStepPotentialApproverDAOToModel } from './timeTrackingCategorizedHourApprovalStepPotentialApproverMapper';

export const timeTrackingCategorizedHourApprovalStepDAOToModel = (
    dao: TimeTrackingCategorizedHourApprovalStepDAO,
): TimeTrackingCategorizedHourApprovalStep => {
    return new TimeTrackingCategorizedHourApprovalStep({
        approverId: dao.approverId,
        categorizedHourId: dao.categorizedHourId,
        comment: dao.comment,
        createdAt: dao.createdAt,
        deletedAt: dao.deletedAt,
        id: dao.id,
        policyApprovalStepId: dao.policyApprovalStepId,
        position: dao.position,
        potentialApprovers: (dao.potentialApprovers ?? []).map(timeTrackingCategorizedHourApprovalStepPotentialApproverDAOToModel),
        resolutionDate: dao.resolutionDate,
        state: dao.state,
        updatedAt: dao.updatedAt,
    });
};

export const timeTrackingCategorizedHourApprovalStepToDAO = (
    model: TimeTrackingCategorizedHourApprovalStep | TimeTrackingCategorizedHourApprovalStepForCreation,
): TimeTrackingCategorizedHourApprovalStepDAO => {
    const isAlreadyCreated = model instanceof TimeTrackingCategorizedHourApprovalStep;
    const dao = TimeTrackingCategorizedHourApprovalStepDAO.build(
        {
            approverId: model.approverId,
            categorizedHourId: model.categorizedHourId,
            comment: model.comment,
            policyApprovalStepId: model.policyApprovalStepId,
            position: model.position,
            resolutionDate: model.resolutionDate,
            state: model.state,
        },
        { isNewRecord: !isAlreadyCreated },
    );
    if (isAlreadyCreated) {
        dao.id = model.id;
        dao.createdAt = model.createdAt;
        dao.updatedAt = model.updatedAt;
        dao.deletedAt = model.deletedAt;
    }
    return dao;
};
