"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.PROMPT_RESOLUTION_PRECEDENCE = exports.CONFIG_PRECEDENCE_ORDER = exports.PERSISTED_CONFIG_KEYS = exports.DERIVED_CONFIG_FIELDS = exports.CONFIG_FIELD_CONTRACTS = exports.BUILT_IN_CONFIG_DEFAULTS = 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");
const monorepo_projects_1 = require("./monorepo-projects");
const config_catalog_1 = require("./config-catalog");
const config_resolved_entries_1 = require("./config-resolved-entries");
const config_contract_data_1 = require("./config-contract-data");
var config_contract_data_2 = require("./config-contract-data");
Object.defineProperty(exports, "BUILT_IN_CONFIG_DEFAULTS", { enumerable: true, get: function () { return config_contract_data_2.BUILT_IN_CONFIG_DEFAULTS; } });
Object.defineProperty(exports, "CONFIG_FIELD_CONTRACTS", { enumerable: true, get: function () { return config_contract_data_2.CONFIG_FIELD_CONTRACTS; } });
Object.defineProperty(exports, "DERIVED_CONFIG_FIELDS", { enumerable: true, get: function () { return config_contract_data_2.DERIVED_CONFIG_FIELDS; } });
Object.defineProperty(exports, "PERSISTED_CONFIG_KEYS", { enumerable: true, get: function () { return config_contract_data_2.PERSISTED_CONFIG_KEYS; } });
exports.CONFIG_PRECEDENCE_ORDER = ['defaults', 'repository', 'local'];
exports.PROMPT_RESOLUTION_PRECEDENCE = ['user', 'latest', 'packaged-default'];
function resolveDayShiftConfig(options) {
    const repositoryConfig = options.repositoryConfig ?? {};
    const localConfig = options.localConfig ?? {};
    const resolvedLayers = (0, config_layer_resolution_1.resolveConfigValuesAndSources)({
        repositoryConfig,
        localConfig,
        defaults: config_contract_data_1.BUILT_IN_CONFIG_DEFAULTS,
    });
    const derived = deriveConfigPaths(options.repositoryContext.root, resolvedLayers.values);
    const projectContext = (0, monorepo_projects_1.resolveMonorepoProjectContextSync)({
        repositoryRoot: options.repositoryContext.root,
        projects: resolvedLayers.values.monorepo_projects,
        explicitProjectId: options.explicitProjectId,
        currentDirectory: options.currentDirectory,
        repositoryDefaultWorkflowLevel: 'structured',
    });
    const contractRevisionImpactBudget = (0, config_layer_resolution_1.resolveContractRevisionImpactBudget)({
        repositoryConfig,
        projectContext,
        repositoryConfigPath: derived.sharedConfigPath,
    });
    return {
        precedence: exports.CONFIG_PRECEDENCE_ORDER,
        values: resolvedLayers.values,
        sources: resolvedLayers.sources,
        derived,
        resolvedEntries: (0, config_resolved_entries_1.buildResolvedConfigEntries)((0, config_catalog_1.getConfigCatalog)(), {
            repositoryConfig,
            values: resolvedLayers.values,
            sources: resolvedLayers.sources,
            derived,
            licenseProvenance: options.licenseProvenance,
            projectContext,
            contractRevisionImpactBudget,
        }),
        projectContext,
        contractRevisionImpactBudget,
    };
}
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 = config_contract_data_1.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; } });
