"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MissingDefaultSystemWorkflowTemplateError = exports.SIDE_WORKFLOW_TEMPLATE_SURFACES = void 0;
exports.getSideWorkflowTemplateSelectorSurfaces = getSideWorkflowTemplateSelectorSurfaces;
exports.buildSideWorkflowTemplateSelectors = buildSideWorkflowTemplateSelectors;
exports.normalizePersistedSideWorkflowTemplateSelector = normalizePersistedSideWorkflowTemplateSelector;
exports.createNoneWorkflowTemplateResolution = createNoneWorkflowTemplateResolution;
exports.createWorkflowTemplateBinding = createWorkflowTemplateBinding;
exports.createUnresolvedWorkflowTemplateBindings = createUnresolvedWorkflowTemplateBindings;
exports.resolveWorkflowTemplateBinding = resolveWorkflowTemplateBinding;
exports.resolveWorkflowTemplateBindings = resolveWorkflowTemplateBindings;
exports.addWorkflowTemplateSelector = addWorkflowTemplateSelector;
exports.addWorkflowTemplateSelectors = addWorkflowTemplateSelectors;
exports.setWorkflowTemplateBinding = setWorkflowTemplateBinding;
exports.createWorkflowTemplateSelectorMap = createWorkflowTemplateSelectorMap;
exports.getWorkflowTemplateSelectors = getWorkflowTemplateSelectors;
exports.buildWorkflowTemplateResolution = buildWorkflowTemplateResolution;
exports.getWorkflowTemplateBinding = getWorkflowTemplateBinding;
exports.getActiveWorkflowTemplateBinding = getActiveWorkflowTemplateBinding;
exports.isWorkflowTemplateBindingSurface = isWorkflowTemplateBindingSurface;
exports.isSideWorkflowTemplateSurface = isSideWorkflowTemplateSurface;
exports.getWorkflowTemplateName = getWorkflowTemplateName;
exports.getWorkflowTemplateDigest = getWorkflowTemplateDigest;
exports.isMissingDefaultSystemWorkflowTemplateError = isMissingDefaultSystemWorkflowTemplateError;
const types_1 = require("./types");
const default_system_workflows_1 = require("./default-system-workflows");
const workflow_template_targets_1 = require("./workflow-template-targets");
exports.SIDE_WORKFLOW_TEMPLATE_SURFACES = types_1.WORKFLOW_TEMPLATE_TARGETS.filter((surface) => surface !== "issue");
function getSideWorkflowTemplateSelectorSurfaces(selectors) {
    if (selectors === undefined) {
        return [];
    }
    return exports.SIDE_WORKFLOW_TEMPLATE_SURFACES.flatMap((surface) => Object.hasOwn(selectors, surface) ? [surface] : []);
}
function buildSideWorkflowTemplateSelectors(input) {
    const selectors = {};
    for (const surface of exports.SIDE_WORKFLOW_TEMPLATE_SURFACES) {
        if (surface === input.excludedSurface) {
            continue;
        }
        const selector = getSideWorkflowTemplateSelector({
            workflowTemplates: input.workflowTemplates,
            surface,
            selectorUpdates: input.selectorUpdates,
            selectorDefaults: input.selectorDefaults,
            includeImplicitAutoSelectors: input.includeImplicitAutoSelectors ?? false,
        });
        if (selector !== undefined) {
            selectors[surface] = selector;
        }
    }
    return selectors;
}
function normalizePersistedSideWorkflowTemplateSelector(selector) {
    return selector === undefined
        ? (0, types_1.createAutoSelectWorkflowTemplateSelector)()
        : selector;
}
function createNoneWorkflowTemplateResolution() {
    return { kind: "none" };
}
function createWorkflowTemplateBinding(input) {
    return {
        selector: input.selector,
        resolution: input.resolution ?? createNoneWorkflowTemplateResolution(),
    };
}
function createUnresolvedWorkflowTemplateBindings(selectors = {}) {
    return Object.fromEntries(types_1.WORKFLOW_TEMPLATE_TARGETS.map((surface) => [
        surface,
        createWorkflowTemplateBinding({
            selector: selectors[surface] ?? (0, types_1.createAutoSelectWorkflowTemplateSelector)(),
        }),
    ]));
}
function resolveWorkflowTemplateBinding(input) {
    return createWorkflowTemplateBinding({
        selector: input.selector,
        resolution: resolveWorkflowTemplateResolution(input),
    });
}
function resolveWorkflowTemplateBindings(input) {
    return Object.fromEntries(types_1.WORKFLOW_TEMPLATE_TARGETS.map((surface) => [
        surface,
        resolveWorkflowTemplateBinding({
            catalog: input.catalog,
            surface,
            selector: input.selectors[surface] ??
                (0, types_1.createAutoSelectWorkflowTemplateSelector)(),
        }),
    ]));
}
function addWorkflowTemplateSelector(input) {
    if (input.selector === undefined) {
        return input.selectors;
    }
    return {
        ...input.selectors,
        [input.surface]: input.selector,
    };
}
function addWorkflowTemplateSelectors(input) {
    return types_1.WORKFLOW_TEMPLATE_TARGETS.reduce((selectors, surface) => addWorkflowTemplateSelector({
        selectors,
        surface,
        selector: input.updates[surface],
    }), input.selectors);
}
function setWorkflowTemplateBinding(input) {
    return {
        ...input.workflowTemplates,
        [input.surface]: input.binding,
    };
}
function createWorkflowTemplateSelectorMap(input) {
    return {
        ...(input.defaultSelectors ?? {}),
        ...getWorkflowTemplateSelectors(input.workflowTemplates),
        ...(input.selectors ?? {}),
    };
}
function getWorkflowTemplateSelectors(workflowTemplates) {
    if (workflowTemplates === null || workflowTemplates === undefined) {
        return {};
    }
    return Object.fromEntries(types_1.WORKFLOW_TEMPLATE_TARGETS.map((surface) => [
        surface,
        workflowTemplates[surface].selector,
    ]));
}
function buildWorkflowTemplateResolution(template) {
    if (template === null) {
        return createNoneWorkflowTemplateResolution();
    }
    return {
        kind: "template",
        templatePath: template.templatePath,
        digest: template.digest,
    };
}
function getWorkflowTemplateBinding(input) {
    return input.workflowTemplates[input.surface];
}
function getSideWorkflowTemplateSelector(input) {
    if (input.selectorUpdates !== undefined &&
        Object.hasOwn(input.selectorUpdates, input.surface)) {
        return input.selectorUpdates[input.surface];
    }
    const selector = input.workflowTemplates?.[input.surface].selector ??
        input.selectorDefaults?.[input.surface];
    if (input.includeImplicitAutoSelectors ||
        (selector?.mode !== "auto" && selector?.mode !== "auto_select")) {
        return selector;
    }
    return undefined;
}
function getActiveWorkflowTemplateBinding(input) {
    if (!isWorkflowTemplateBindingSurface(input.workflowSurface)) {
        return null;
    }
    return getWorkflowTemplateBinding({
        workflowTemplates: input.workflowTemplates,
        surface: input.workflowSurface,
    });
}
function isWorkflowTemplateBindingSurface(surface) {
    return types_1.WORKFLOW_TEMPLATE_TARGETS.includes(surface);
}
function isSideWorkflowTemplateSurface(surface) {
    return exports.SIDE_WORKFLOW_TEMPLATE_SURFACES.includes(surface);
}
function getWorkflowTemplateName(binding) {
    return binding?.resolution.kind === "template"
        ? binding.resolution.templatePath
        : null;
}
function getWorkflowTemplateDigest(binding) {
    return binding?.resolution.kind === "template"
        ? binding.resolution.digest
        : null;
}
class MissingDefaultSystemWorkflowTemplateError extends Error {
    surface;
    templatePath;
    constructor(input) {
        super(`System default workflow template is unavailable for ${input.surface}: ${input.templatePath}`);
        this.name = "MissingDefaultSystemWorkflowTemplateError";
        this.surface = input.surface;
        this.templatePath = input.templatePath;
    }
}
exports.MissingDefaultSystemWorkflowTemplateError = MissingDefaultSystemWorkflowTemplateError;
function isMissingDefaultSystemWorkflowTemplateError(error) {
    return error instanceof MissingDefaultSystemWorkflowTemplateError;
}
function resolveWorkflowTemplateResolution(input) {
    switch (input.selector.mode) {
        case "auto":
        case "auto_select":
            return createNoneWorkflowTemplateResolution();
        case "no_template": {
            const template = (0, default_system_workflows_1.resolveDefaultSystemWorkflowTemplate)({
                catalog: input.catalog,
                surface: input.surface,
            });
            if (template === null) {
                throw new MissingDefaultSystemWorkflowTemplateError({
                    surface: input.surface,
                    templatePath: (0, default_system_workflows_1.resolveDefaultSystemWorkflowTemplatePath)(input.surface) ??
                        "default system workflow",
                });
            }
            return buildWorkflowTemplateResolution(template);
        }
        case "template": {
            const template = input.catalog.templateByPath.get(input.selector.templatePath);
            return template === undefined ||
                !(0, workflow_template_targets_1.workflowTemplateTargetsSurface)(template, input.surface)
                ? createNoneWorkflowTemplateResolution()
                : buildWorkflowTemplateResolution(template);
        }
    }
}
