import {
    TimeTrackingPolicyApprovalStepResponsibleRelation,
    TimeTrackingPolicyApprovalStepResponsibleRelationForCreation,
} from '../../../../../business/models/policies/approvalSteps/responsibles/relation';
import { TimeTrackingPolicyApprovalStepResponsibleRelationDAO } from '../../../../dataAccessObjects/policies/approvalSteps/responsibles/relation';
import { timeTrackingPolicyApprovalStepDAOToModel } from '../timeTrackingPolicyApprovalStepMapper';

export const timeTrackingPolicyApprovalStepResponsibleRelationDAOToModel = (dao: TimeTrackingPolicyApprovalStepResponsibleRelationDAO) => {
    return new TimeTrackingPolicyApprovalStepResponsibleRelation({
        approvalStep: timeTrackingPolicyApprovalStepDAOToModel(dao.approvalStep),
        approvalStepId: dao.approvalStepId,
        createdAt: dao.createdAt,
        deletedAt: dao.deletedAt,
        id: dao.id,
        relation: dao.relation,
        updatedAt: dao.updatedAt,
    });
};

export const timeTrackingPolicyApprovalStepResponsibleRelationModelToDAO = (
    responsibleRelation: TimeTrackingPolicyApprovalStepResponsibleRelation | TimeTrackingPolicyApprovalStepResponsibleRelationForCreation,
) => {
    const isAlreadyCreated = responsibleRelation instanceof TimeTrackingPolicyApprovalStepResponsibleRelation;
    const dao = TimeTrackingPolicyApprovalStepResponsibleRelationDAO.build(
        {
            approvalStepId: responsibleRelation.approvalStepId,
            relation: responsibleRelation.relation,
        },
        { isNewRecord: !isAlreadyCreated },
    );
    if (isAlreadyCreated) {
        dao.id = responsibleRelation.id;
        dao.createdAt = responsibleRelation.createdAt;
        dao.updatedAt = responsibleRelation.updatedAt;
        dao.deletedAt = responsibleRelation.deletedAt;
    }
    return dao;
};
