import {
    TimeTrackingPolicyApprovalStepResponsibleUser,
    TimeTrackingPolicyApprovalStepResponsibleUserForCreation,
} from '../../../../../business/models/policies/approvalSteps/responsibles/user';
import { TimeTrackingPolicyApprovalStepResponsibleUserDAO } from '../../../../dataAccessObjects/policies/approvalSteps/responsibles/user';
import { timeTrackingPolicyApprovalStepDAOToModel } from '../timeTrackingPolicyApprovalStepMapper';

export const timeTrackingPolicyApprovalStepResponsibleUserDAOToModel = (dao: TimeTrackingPolicyApprovalStepResponsibleUserDAO) => {
    return new TimeTrackingPolicyApprovalStepResponsibleUser({
        approvalStep: timeTrackingPolicyApprovalStepDAOToModel(dao.approvalStep),
        approvalStepId: dao.approvalStepId,
        createdAt: dao.createdAt,
        deletedAt: dao.deletedAt,
        id: dao.id,
        updatedAt: dao.updatedAt,
        userId: dao.userId,
    });
};

export const timeTrackingPolicyApprovalStepResponsibleUserModelToDAO = (
    responsibleUser: TimeTrackingPolicyApprovalStepResponsibleUser | TimeTrackingPolicyApprovalStepResponsibleUserForCreation,
) => {
    const isAlreadyCreated = responsibleUser instanceof TimeTrackingPolicyApprovalStepResponsibleUser;
    const dao = TimeTrackingPolicyApprovalStepResponsibleUserDAO.build(
        {
            approvalStepId: responsibleUser.approvalStepId,
            userId: responsibleUser.userId,
        },
        { isNewRecord: !isAlreadyCreated },
    );
    if (isAlreadyCreated) {
        dao.id = responsibleUser.id;
        dao.createdAt = responsibleUser.createdAt;
        dao.updatedAt = responsibleUser.updatedAt;
        dao.deletedAt = responsibleUser.deletedAt;
    }
    return dao;
};
