"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TEMPLATE_MANIFEST_PATH = exports.CANONICAL_TEMPLATE_ROOT = void 0;
exports.resolveWorkspaceTemplateAbsolutePath = resolveWorkspaceTemplateAbsolutePath;
exports.resolveWorkspaceTemplateAbsolutePathFromDayShiftDirectory = resolveWorkspaceTemplateAbsolutePathFromDayShiftDirectory;
const node_path_1 = __importDefault(require("node:path"));
const config_contract_1 = require("../config/config-contract");
const paths_1 = require("../paths");
exports.CANONICAL_TEMPLATE_ROOT = ".day-shift/templates";
exports.TEMPLATE_MANIFEST_PATH = `${exports.CANONICAL_TEMPLATE_ROOT}/manifest.json`;
function resolveWorkspaceTemplateAbsolutePath(options) {
    const resolvedConfig = (0, config_contract_1.resolveDayShiftConfig)({
        repositoryContext: createExplicitRepositoryContext(options.repositoryRoot)
    });
    return resolveWorkspaceTemplateAbsolutePathFromDayShiftDirectory({
        dayShiftDirectoryPath: resolvedConfig.derived.dayShiftDirectoryPath,
        templatePath: options.templatePath
    });
}
function resolveWorkspaceTemplateAbsolutePathFromDayShiftDirectory(options) {
    const normalizedTemplatePath = (0, paths_1.parseRepositoryRelativePath)(options.templatePath).normalizedPath;
    if (normalizedTemplatePath === exports.CANONICAL_TEMPLATE_ROOT) {
        return node_path_1.default.join(options.dayShiftDirectoryPath, "templates");
    }
    const prefix = `${exports.CANONICAL_TEMPLATE_ROOT}/`;
    if (!normalizedTemplatePath.startsWith(prefix)) {
        return node_path_1.default.resolve(options.dayShiftDirectoryPath, normalizedTemplatePath);
    }
    return node_path_1.default.join(options.dayShiftDirectoryPath, "templates", normalizedTemplatePath.slice(prefix.length));
}
function createExplicitRepositoryContext(repositoryRoot) {
    return {
        root: repositoryRoot,
        detectionSource: "explicit",
        warnedAboutFallback: false
    };
}
