"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.projectWorkUnitDisposition = projectWorkUnitDisposition;
exports.appendInitialWorkUnitFindings = appendInitialWorkUnitFindings;
exports.workUnitIsBlocked = workUnitIsBlocked;
exports.findUnsupportedWorkUnitContent = findUnsupportedWorkUnitContent;
exports.buildWorkUnitExpectedConditions = buildWorkUnitExpectedConditions;
const ORDERED_SLICE_ID_PATTERN = /^(?:0\d|[1-9]\d+)-[a-z0-9]+(?:-[a-z0-9]+)*$/;
function projectWorkUnitDisposition(input) {
    const selectedWorkflowLevel = input.explicitWorkflowLevel ??
        input.proposedWorkflowLevel ??
        input.canonicalWorkflowLevel ??
        input.physicalWorkflowLevel;
    const selectionSource = input.explicitWorkflowLevel !== undefined
        ? 'explicit'
        : input.proposedWorkflowLevel === null &&
            input.canonicalWorkflowLevel === null &&
            input.physicalWorkflowLevel === null
            ? null
            : 'projection';
    const alreadyCanonical = input.canonicalWorkflowLevel !== null &&
        selectedWorkflowLevel === input.canonicalWorkflowLevel &&
        (selectedWorkflowLevel === 'governed' || input.implementationOrderSource === 'existing-prefix') &&
        (selectedWorkflowLevel !== 'structured' || input.structuredTaskPathsCanonical);
    const governedSliceIdentityValid = selectedWorkflowLevel !== 'governed' || (input.sliceId !== null && ORDERED_SLICE_ID_PATTERN.test(input.sliceId));
    return {
        selectedWorkflowLevel,
        selectionSource,
        alreadyCanonical,
        governedSliceIdentityValid,
    };
}
function appendInitialWorkUnitFindings(unit, selectedWorkflowLevel, governedSliceIdentityValid, sourcePaths, unsafePaths, findings, createFinding) {
    if (selectedWorkflowLevel === null) {
        findings.push(createFinding('AMBIGUOUS_MAPPING', unit.sourcePaths, `Work unit '${unit.workUnitId}' has no selected workflow level.`, `Supply --mapping ${unit.workUnitId}=<basic|structured|governed> after reviewing its ambiguity evidence.`));
    }
    if (unit.ambiguityReasons.some((reason) => reason.code === 'duplicate-active-identity')) {
        findings.push(createFinding('DUPLICATE_ACTIVE_LOCATION', unit.sourcePaths, `Work unit '${unit.workUnitId}' contains a duplicate active task identity.`, 'Retire or reconcile the duplicate active location, then refresh the migration plan.'));
    }
    if (!governedSliceIdentityValid) {
        findings.push(createFinding('INVALID_SLICE_IDENTITY', sourcePaths, `Governed work unit '${unit.workUnitId}' does not have one canonical ordered slice identity with a prefix of at least two digits.`, 'Repair the declared and physical slice identity, then refresh the no-write migration plan.'));
    }
    for (const unsafePath of unsafePaths) {
        findings.push(createFinding('UNSAFE_PATH', [unsafePath], `Work unit '${unit.workUnitId}' contains a symlink, non-regular entry, or unsafe source path.`, 'Replace the unsafe entry with regular repository-owned content and refresh the migration plan.'));
    }
}
function workUnitIsBlocked(input) {
    return (input.selectedWorkflowLevel === null ||
        ((input.selectedWorkflowLevel === 'basic' || input.selectedWorkflowLevel === 'structured') &&
            !input.orderingPresent) ||
        (input.selectedWorkflowLevel === 'structured' && input.structuredTaskSequenceMissing) ||
        input.duplicateActiveIdentity ||
        !input.governedSliceIdentityValid ||
        input.sourceInventoryUnsafe ||
        input.governedSharedEvidenceMissing ||
        input.unsupportedContentPresent ||
        input.unsafePathsPresent);
}
function findUnsupportedWorkUnitContent(sourcePaths, artifactMappings, selectedWorkflowLevel) {
    const mappedSources = new Set(artifactMappings.flatMap((mapping) => (mapping.sourcePath === null ? [] : [mapping.sourcePath])));
    return sourcePaths
        .filter((sourcePath) => !mappedSources.has(sourcePath))
        .map((sourcePath) => ({
        code: 'unsupported-unmapped-source',
        field: 'path',
        paths: [sourcePath],
        message: `Source artifact '${sourcePath}' has no preservation mapping for selected level '${selectedWorkflowLevel ?? '(missing)'}'.`,
    }));
}
function buildWorkUnitExpectedConditions(input, readRevision, uniqueExpected) {
    if (input.alreadyCanonical)
        return [];
    const sourcePaths = [
        ...input.sourcePaths,
        ...(input.sharedEvidenceSourcePath === null ? [] : [input.sharedEvidenceSourcePath]),
    ];
    return uniqueExpected([
        ...sourcePaths.flatMap((sourcePath) => {
            const revision = readRevision(sourcePath);
            return revision === null
                ? []
                : [
                    {
                        path: sourcePath,
                        kind: 'destination-presence',
                        expectedState: 'present',
                    },
                    {
                        path: sourcePath,
                        kind: 'content-revision',
                        expectedState: revision,
                    },
                ];
        }),
        ...input.mappings.map((mapping) => ({
            path: mapping.destinationPath,
            kind: 'destination-presence',
            expectedState: 'absent',
        })),
    ]);
}
