"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.tryLoadWorkflowDisplayCatalog = tryLoadWorkflowDisplayCatalog;
exports.resolveWorkflowDisplayLabelsForRepo = resolveWorkflowDisplayLabelsForRepo;
exports.resolveWorkflowDisplayLabels = resolveWorkflowDisplayLabels;
exports.resolveWorkflowTemplateDisplayLabel = resolveWorkflowTemplateDisplayLabel;
const catalog_1 = require("../i18n/catalog");
const optional_text_1 = require("./optional-text");
const template_bindings_1 = require("./template-bindings");
const template_registry_1 = require("./template-registry");
const surface_record_1 = require("./surface-record");
async function tryLoadWorkflowDisplayCatalog(repoRoot, locale = catalog_1.DEFAULT_APP_LOCALE) {
    try {
        return await (0, template_registry_1.loadWorkflowTemplates)((0, template_registry_1.getWorkflowTemplatesDirectoryPath)(repoRoot), locale);
    }
    catch {
        return null;
    }
}
async function resolveWorkflowDisplayLabelsForRepo(input) {
    return resolveWorkflowDisplayLabels({
        catalog: await tryLoadWorkflowDisplayCatalog(input.repoRoot, input.locale ?? catalog_1.DEFAULT_APP_LOCALE),
        workflowTemplates: input.workflowTemplates,
        activeSurface: input.activeSurface,
    });
}
function resolveWorkflowDisplayLabels(input) {
    const activeSurface = input.activeSurface ?? "issue";
    const workflowTemplateLabelsBySurface = resolveWorkflowTemplateDisplayLabelsBySurface(input);
    return {
        workflowTemplateLabel: input.workflowTemplates === undefined || input.workflowTemplates === null
            ? workflowTemplateLabelsBySurface[activeSurface]
            : activeSurface === "merge"
                ? null
                : resolveTemplateDisplayLabel({
                    binding: (0, template_bindings_1.getActiveWorkflowTemplateBinding)({
                        workflowSurface: activeSurface,
                        workflowTemplates: input.workflowTemplates,
                    }),
                }),
        workflowTemplateLabelsBySurface,
    };
}
function resolveWorkflowTemplateDisplayLabelsBySurface(input) {
    return (0, surface_record_1.createWorkflowSurfaceRecord)((surface) => resolveWorkflowTemplateDisplayLabel({
        catalog: input.catalog,
        workflowTemplates: input.workflowTemplates,
        surface,
    }));
}
function resolveWorkflowTemplateDisplayLabel(input) {
    const surface = input.surface ?? "issue";
    const binding = input.workflowTemplates === undefined || input.workflowTemplates === null
        ? undefined
        : surface === "merge"
            ? undefined
            : (0, template_bindings_1.isWorkflowTemplateBindingSurface)(surface)
                ? (0, template_bindings_1.getWorkflowTemplateBinding)({
                    workflowTemplates: input.workflowTemplates,
                    surface,
                })
                : (0, template_bindings_1.getActiveWorkflowTemplateBinding)({
                    workflowSurface: surface,
                    workflowTemplates: input.workflowTemplates,
                });
    return resolveTemplateDisplayLabel({
        binding,
    });
}
function resolveTemplateDisplayLabel(input) {
    const resolutionLabel = resolveResolutionDisplayLabel(input.binding?.selector, input.binding?.resolution);
    if (resolutionLabel !== undefined) {
        return resolutionLabel;
    }
    const selectorLabel = resolveSelectorDisplayLabel(input.binding?.selector);
    if (selectorLabel?.mode === "no_template") {
        return null;
    }
    return selectorLabel?.templatePath ?? selectorLabel?.fallbackLabel ?? null;
}
function resolveResolutionDisplayLabel(selector, resolution) {
    if (resolution === undefined || resolution === null) {
        return undefined;
    }
    switch (resolution.kind) {
        case "template":
            return (0, optional_text_1.normalizeOptionalText)(resolution.templatePath);
        case "none":
            return selector?.mode === "no_template" ? null : undefined;
    }
}
function resolveSelectorDisplayLabel(selector) {
    if (selector === undefined || selector === null) {
        return null;
    }
    switch (selector.mode) {
        case "template":
            return { mode: "selected", templatePath: selector.templatePath };
        case "no_template":
            return { mode: "no_template" };
        case "auto_select":
            return { mode: "selected", fallbackLabel: "auto_select" };
        case "auto":
            return { mode: "selected", fallbackLabel: "auto" };
    }
}
