"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveRepositoryRelativePath = exports.DERIVED_CONFIG_FIELDS = exports.PERSISTED_CONFIG_KEYS = exports.CONFIG_FIELD_CONTRACTS = exports.BUILT_IN_CONFIG_DEFAULTS = exports.PROMPT_RESOLUTION_PRECEDENCE = exports.CONFIG_PRECEDENCE_ORDER = void 0;
exports.resolveDayShiftConfig = resolveDayShiftConfig;
exports.deriveConfigPaths = deriveConfigPaths;
exports.classifyPromptWorkspaceLayer = classifyPromptWorkspaceLayer;
const node_path_1 = __importDefault(require("node:path"));
const runtime_contract_1 = require("../runtime/runtime-contract");
const paths_1 = require("../paths");
const config_layer_resolution_1 = require("./config-layer-resolution");
exports.CONFIG_PRECEDENCE_ORDER = ["defaults", "repository", "local"];
exports.PROMPT_RESOLUTION_PRECEDENCE = ["user", "latest", "packaged-default"];
exports.BUILT_IN_CONFIG_DEFAULTS = {
    planning_root: node_path_1.default.posix.join(runtime_contract_1.DAY_SHIFT_DIRECTORY_NAME, "planning"),
    spec_root: "specs",
    spec_index: node_path_1.default.posix.join(runtime_contract_1.DAY_SHIFT_DIRECTORY_NAME, "specs", "index.json"),
    agent_entrypoint: node_path_1.default.posix.join(runtime_contract_1.DAY_SHIFT_DIRECTORY_NAME, runtime_contract_1.AGENT_ENTRYPOINT_FILE_NAME),
    default_readiness: "needs more evidence",
    config_schema_version: 1,
    spec_index_schema_version: 1,
    template_version: 1,
    prompt_version: 1,
    prompt_root: node_path_1.default.posix.join(runtime_contract_1.DAY_SHIFT_DIRECTORY_NAME, "prompts"),
    prompt_latest_root: node_path_1.default.posix.join(runtime_contract_1.DAY_SHIFT_DIRECTORY_NAME, "prompts", "latest"),
    prompt_user_root: node_path_1.default.posix.join(runtime_contract_1.DAY_SHIFT_DIRECTORY_NAME, "prompts", "user"),
    cli_command: "day-shift",
    cli_resolution: "installed",
    cli_verified: true,
    cli_verified_via: "day-shift --help"
};
exports.CONFIG_FIELD_CONTRACTS = {
    planning_root: {
        key: "planning_root",
        mutability: "repository-owned",
        writableThroughConfigSet: false,
        allowedLayers: ["defaults", "repository"]
    },
    spec_root: {
        key: "spec_root",
        mutability: "repository-owned",
        writableThroughConfigSet: false,
        allowedLayers: ["defaults", "repository"]
    },
    spec_index: {
        key: "spec_index",
        mutability: "repository-owned",
        writableThroughConfigSet: false,
        allowedLayers: ["defaults", "repository"]
    },
    agent_entrypoint: {
        key: "agent_entrypoint",
        mutability: "repository-owned",
        writableThroughConfigSet: false,
        allowedLayers: ["defaults", "repository"]
    },
    default_readiness: {
        key: "default_readiness",
        mutability: "mutable-shared",
        writableThroughConfigSet: true,
        allowedLayers: ["defaults", "repository"]
    },
    config_schema_version: {
        key: "config_schema_version",
        mutability: "read-only-metadata",
        writableThroughConfigSet: false,
        allowedLayers: ["defaults", "repository"]
    },
    spec_index_schema_version: {
        key: "spec_index_schema_version",
        mutability: "read-only-metadata",
        writableThroughConfigSet: false,
        allowedLayers: ["defaults", "repository"]
    },
    template_version: {
        key: "template_version",
        mutability: "read-only-metadata",
        writableThroughConfigSet: false,
        allowedLayers: ["defaults", "repository"]
    },
    prompt_version: {
        key: "prompt_version",
        mutability: "read-only-metadata",
        writableThroughConfigSet: false,
        allowedLayers: ["defaults", "repository"]
    },
    prompt_root: {
        key: "prompt_root",
        mutability: "repository-owned",
        writableThroughConfigSet: false,
        allowedLayers: ["defaults", "repository"]
    },
    prompt_latest_root: {
        key: "prompt_latest_root",
        mutability: "repository-owned",
        writableThroughConfigSet: false,
        allowedLayers: ["defaults", "repository"]
    },
    prompt_user_root: {
        key: "prompt_user_root",
        mutability: "repository-owned",
        writableThroughConfigSet: false,
        allowedLayers: ["defaults", "repository"]
    },
    cli_command: {
        key: "cli_command",
        mutability: "mutable-shared",
        writableThroughConfigSet: true,
        allowedLayers: ["defaults", "repository"]
    },
    cli_resolution: {
        key: "cli_resolution",
        mutability: "mutable-shared",
        writableThroughConfigSet: true,
        allowedLayers: ["defaults", "repository"]
    },
    cli_verified: {
        key: "cli_verified",
        mutability: "mutable-shared",
        writableThroughConfigSet: true,
        allowedLayers: ["defaults", "repository"]
    },
    cli_verified_via: {
        key: "cli_verified_via",
        mutability: "mutable-shared",
        writableThroughConfigSet: true,
        allowedLayers: ["defaults", "repository"]
    },
    version: {
        key: "version",
        mutability: "mutable-shared",
        writableThroughConfigSet: true,
        allowedLayers: ["repository", "local"]
    },
    editor: {
        key: "editor",
        mutability: "local-only",
        writableThroughConfigSet: true,
        allowedLayers: ["defaults", "local"]
    }
};
exports.PERSISTED_CONFIG_KEYS = Object.freeze(Object.keys(exports.CONFIG_FIELD_CONTRACTS));
exports.DERIVED_CONFIG_FIELDS = Object.freeze([
    "repositoryRoot",
    "dayShiftDirectoryPath",
    "sharedConfigPath",
    "localConfigPath",
    "planningRootPath",
    "specRootPath",
    "specIndexPath",
    "agentEntrypointPath",
    "promptRootPath",
    "promptLatestRootPath",
    "promptUserRootPath"
]);
function resolveDayShiftConfig(options) {
    const repositoryConfig = options.repositoryConfig ?? {};
    const localConfig = options.localConfig ?? {};
    const resolvedLayers = (0, config_layer_resolution_1.resolveConfigValuesAndSources)({
        repositoryConfig,
        localConfig,
        defaults: exports.BUILT_IN_CONFIG_DEFAULTS
    });
    const derived = deriveConfigPaths(options.repositoryContext.root, resolvedLayers.values);
    const { buildResolvedConfigEntries } = require("./config-resolved-entries");
    return {
        precedence: exports.CONFIG_PRECEDENCE_ORDER,
        values: resolvedLayers.values,
        sources: resolvedLayers.sources,
        derived,
        resolvedEntries: buildResolvedConfigEntries({
            repositoryConfig,
            values: resolvedLayers.values,
            sources: resolvedLayers.sources,
            derived,
            licenseProvenance: options.licenseProvenance
        })
    };
}
function deriveConfigPaths(repositoryRoot, values) {
    const dayShiftDirectoryPath = node_path_1.default.join(repositoryRoot, runtime_contract_1.DAY_SHIFT_DIRECTORY_NAME);
    return {
        repositoryRoot,
        dayShiftDirectoryPath,
        sharedConfigPath: node_path_1.default.join(dayShiftDirectoryPath, runtime_contract_1.CONFIG_FILE_NAME),
        localConfigPath: node_path_1.default.join(dayShiftDirectoryPath, runtime_contract_1.LOCAL_CONFIG_FILE_NAME),
        planningRootPath: (0, paths_1.resolveRepositoryRelativePath)(repositoryRoot, values.planning_root),
        specRootPath: (0, paths_1.resolveRepositoryRelativePath)(repositoryRoot, values.spec_root),
        specIndexPath: (0, paths_1.resolveRepositoryRelativePath)(repositoryRoot, values.spec_index),
        agentEntrypointPath: (0, paths_1.resolveRepositoryRelativePath)(repositoryRoot, values.agent_entrypoint),
        promptRootPath: (0, paths_1.resolveRepositoryRelativePath)(repositoryRoot, values.prompt_root),
        promptLatestRootPath: (0, paths_1.resolveRepositoryRelativePath)(repositoryRoot, values.prompt_latest_root),
        promptUserRootPath: (0, paths_1.resolveRepositoryRelativePath)(repositoryRoot, values.prompt_user_root)
    };
}
function classifyPromptWorkspaceLayer(promptPath, values = exports.BUILT_IN_CONFIG_DEFAULTS) {
    const normalizedPromptPath = (0, paths_1.parseRepositoryRelativePath)(promptPath).normalizedPath;
    if (normalizedPromptPath.startsWith(`${values.prompt_user_root}/`)) {
        return "user";
    }
    if (normalizedPromptPath.startsWith(`${values.prompt_latest_root}/`)) {
        return "latest";
    }
    if (normalizedPromptPath.startsWith(`${values.prompt_root}/`)) {
        return "shared-root";
    }
    return "outside";
}
var paths_2 = require("../paths");
Object.defineProperty(exports, "resolveRepositoryRelativePath", { enumerable: true, get: function () { return paths_2.resolveRepositoryRelativePath; } });
