import {
    TimeTrackingPolicyApprovalStepResponsibleSegmentationItem,
    TimeTrackingPolicyApprovalStepResponsibleSegmentationItemForCreation,
} from '../../../../../business/models/policies/approvalSteps/responsibles/segmentationItem';
import { TimeTrackingPolicyApprovalStepResponsibleSegmentationItemDAO } from '../../../../dataAccessObjects/policies/approvalSteps/responsibles/segmentationItem';
import { timeTrackingPolicyApprovalStepDAOToModel } from '../timeTrackingPolicyApprovalStepMapper';

export const timeTrackingPolicyApprovalStepResponsibleSegmentationItemDAOToModel = (
    dao: TimeTrackingPolicyApprovalStepResponsibleSegmentationItemDAO,
) => {
    return new TimeTrackingPolicyApprovalStepResponsibleSegmentationItem({
        approvalStep: timeTrackingPolicyApprovalStepDAOToModel(dao.approvalStep),
        approvalStepId: dao.approvalStepId,
        createdAt: dao.createdAt,
        deletedAt: dao.deletedAt,
        id: dao.id,
        segmentationItemId: dao.segmentationItemId,
        updatedAt: dao.updatedAt,
    });
};

export const timeTrackingPolicyApprovalStepResponsibleSegmentationItemModelToDAO = (
    responsibleSegmentationItem:
        | TimeTrackingPolicyApprovalStepResponsibleSegmentationItem
        | TimeTrackingPolicyApprovalStepResponsibleSegmentationItemForCreation,
) => {
    const isAlreadyCreated = responsibleSegmentationItem instanceof TimeTrackingPolicyApprovalStepResponsibleSegmentationItem;
    const dao = TimeTrackingPolicyApprovalStepResponsibleSegmentationItemDAO.build(
        {
            approvalStepId: responsibleSegmentationItem.approvalStepId,
            segmentationItemId: responsibleSegmentationItem.segmentationItemId,
        },
        { isNewRecord: !isAlreadyCreated },
    );
    if (isAlreadyCreated) {
        dao.id = responsibleSegmentationItem.id;
        dao.createdAt = responsibleSegmentationItem.createdAt;
        dao.updatedAt = responsibleSegmentationItem.updatedAt;
        dao.deletedAt = responsibleSegmentationItem.deletedAt;
    }
    return dao;
};
