"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.normalizeTrackerIssueListQuery = normalizeTrackerIssueListQuery;
exports.normalizeServiceManagedDeltaIssueListQuery = normalizeServiceManagedDeltaIssueListQuery;
exports.normalizeTrackerMilestoneRef = normalizeTrackerMilestoneRef;
exports.normalizeTrackerProjectRef = normalizeTrackerProjectRef;
exports.buildTrackerWorkflowStateSyncInput = buildTrackerWorkflowStateSyncInput;
exports.normalizeTrackerIssue = normalizeTrackerIssue;
exports.mapNormalizedTrackerIssue = mapNormalizedTrackerIssue;
exports.normalizeMappedTrackerIssueRecord = normalizeMappedTrackerIssueRecord;
exports.collectMappedTrackerIssues = collectMappedTrackerIssues;
exports.normalizeTrackerIssueRecord = normalizeTrackerIssueRecord;
exports.mapMutatedTrackerIssueOrThrow = mapMutatedTrackerIssueOrThrow;
const title_progress_1 = require("./title-progress");
function normalizeTrackerIssueListQuery(query) {
    return {
        ...query,
        ...(query.labelNames ? { labelNames: [...query.labelNames] } : {}),
        includeSourceBranchName: query.includeSourceBranchName ?? false,
    };
}
function normalizeServiceManagedDeltaIssueListQuery(query) {
    return {
        ...query,
        includeSourceBranchName: query.includeSourceBranchName ?? false,
    };
}
function normalizeTrackerMilestoneRef(milestoneRef) {
    const normalized = milestoneRef?.trim();
    return normalized ? normalized : null;
}
function normalizeTrackerProjectRef(projectRef) {
    const normalized = projectRef?.trim();
    return normalized ? normalized : null;
}
function buildTrackerWorkflowStateSyncInput(input, currentLabelNames) {
    if (currentLabelNames === undefined) {
        return input;
    }
    return {
        ...input,
        currentLabelNames,
    };
}
function normalizeTrackerIssue(input, options) {
    const issueRecord = normalizeTrackerIssueRecord(input);
    const workflowState = options.parseWorkflowState(input.labelNames);
    if (!workflowState) {
        return null;
    }
    return {
        ...issueRecord,
        workflowState,
    };
}
async function mapNormalizedTrackerIssue(input) {
    const issueRecord = await input.mapIssueRecord(input.issue, {
        includeSourceBranchName: input.options.includeSourceBranchName,
    });
    return normalizeMappedTrackerIssueRecord({
        issueRecord,
        requireWorkflowState: input.options.requireWorkflowState,
        parseWorkflowState: input.parseWorkflowState,
    });
}
function normalizeMappedTrackerIssueRecord(input) {
    return normalizeTrackerIssue({
        ...input.issueRecord,
        labelNames: input.issueRecord.labelNames ?? [],
    }, {
        requireWorkflowState: input.requireWorkflowState,
        parseWorkflowState: input.parseWorkflowState,
    });
}
function collectMappedTrackerIssues(mappedIssues, workflowStatus) {
    return mappedIssues
        .flatMap((issue) => (issue ? [issue] : []))
        .filter((issue) => workflowStatus ? issue.workflowState.status === workflowStatus : true);
}
function normalizeTrackerIssueRecord(input) {
    const sourceBranchName = normalizeOptionalTrackerText(input.sourceBranchName);
    const milestoneRef = normalizeTrackerMilestoneRef(input.milestoneRef);
    const projectRef = normalizeTrackerProjectRef(input.projectRef);
    const issue = {
        id: input.id,
        displayId: input.displayId,
        title: input.title,
        baseTitle: (0, title_progress_1.stripIssueTitleProgressPrefix)(input.title),
        body: input.body ?? "",
        url: input.url,
        author: input.author,
        kind: input.kind,
        labelNames: input.labelNames,
        assignees: input.assignees,
        isOpen: input.isOpen,
        updatedAt: input.updatedAt,
    };
    if (input.completedAt !== undefined && input.completedAt !== null) {
        issue.completedAt = input.completedAt;
    }
    if (input.pullRequestNumber !== undefined &&
        input.pullRequestNumber !== null) {
        issue.pullRequestNumber = input.pullRequestNumber;
    }
    if (sourceBranchName !== undefined) {
        issue.sourceBranchName = sourceBranchName;
    }
    if (milestoneRef !== null) {
        issue.milestoneRef = milestoneRef;
    }
    if (projectRef !== null) {
        issue.projectRef = projectRef;
    }
    return issue;
}
async function mapMutatedTrackerIssueOrThrow(input) {
    const mapped = await input.mapIssue(input.issue, {
        requireWorkflowState: true,
        includeSourceBranchName: true,
    });
    if (!mapped) {
        throw input.createMissingWorkflowLabelSetError(input.issueId);
    }
    return input.afterMap ? await input.afterMap(mapped) : mapped;
}
function normalizeOptionalTrackerText(value) {
    const normalized = value?.trim();
    return normalized ? normalized : undefined;
}
