import { describe, expect, it } from 'vitest';

import {
  applyApplicantDeptRestriction,
  isApplicantDeptAssigneeTypeDisabled,
  needsApplicantDeptRestrictionConfirmation,
  normalizeApplicantDeptRestriction,
  serializeApplicantDeptRestriction,
} from './approval-flow-restriction';

describe('approval flow applicant department restriction', () => {
  it('normalizes edit values and serializes save values', () => {
    expect(normalizeApplicantDeptRestriction(1)).toBe(true);
    expect(normalizeApplicantDeptRestriction(undefined)).toBe(false);
    expect(serializeApplicantDeptRestriction(true)).toBe(1);
    expect(serializeApplicantDeptRestriction(false)).toBe(0);
  });

  it('switches a department step to role and clears department targets', () => {
    const step = {
      assigneeTargetIds: [21],
      assigneeTargets: [{ targetId: 21 }],
      assigneeType: 'DEPT' as const,
      includeChildrenByTarget: { '21': true },
      restrictToApplicantDept: false,
    };

    expect(needsApplicantDeptRestrictionConfirmation(step, true)).toBe(true);
    applyApplicantDeptRestriction(step, true);

    expect(step).toEqual({
      assigneeTargetIds: [],
      assigneeTargets: [],
      assigneeType: 'ROLE',
      includeChildrenByTarget: {},
      restrictToApplicantDept: true,
    });
    expect(isApplicantDeptAssigneeTypeDisabled(true, 'DEPT')).toBe(true);
    expect(isApplicantDeptAssigneeTypeDisabled(true, 'ROLE')).toBe(false);
  });
});
