"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.detectArtifactFamily = detectArtifactFamily;
const node_path_1 = __importDefault(require("node:path"));
const planning_document_utils_1 = require("../planning/planning-document-utils");
const PLANNING_ARTIFACT_TYPES = new Set([
    "slice_overview",
    "phase_overview",
    "milestone_overview",
    "task_definition"
]);
function detectArtifactFamily(input) {
    const normalizedPath = normalizeArtifactPath(input.path);
    const contentFormat = detectContentFormat(normalizedPath);
    const artifactType = contentFormat === "markdown" && input.content !== undefined
        ? readArtifactType(input.content)
        : null;
    const statusFields = contentFormat === "markdown" && input.content !== undefined
        ? readStatusFields(input.content)
        : [];
    const sourceTemplate = isTemplateSourcePath(normalizedPath);
    const scaffoldSource = isScaffoldSourcePath(normalizedPath);
    if (normalizedPath === ".day-shift/prompts/user/manifest.json") {
        return supported(normalizedPath, "prompt-manifest", "json", false, sourceTemplate, scaffoldSource, artifactType, statusFields);
    }
    if (normalizedPath === ".day-shift/templates/manifest.json") {
        return supported(normalizedPath, "template-manifest", "json", false, sourceTemplate, scaffoldSource, artifactType, statusFields);
    }
    if (sourceTemplate) {
        return supported(normalizedPath, "template", contentFormat, true, true, scaffoldSource, artifactType, statusFields);
    }
    if (scaffoldSource) {
        return supported(normalizedPath, "scaffold", contentFormat, contentFormat === "markdown", sourceTemplate, true, artifactType, statusFields);
    }
    if (artifactType !== null) {
        const family = familyFromArtifactType(artifactType);
        if (family !== null) {
            return supported(normalizedPath, family, contentFormat, true, sourceTemplate, scaffoldSource, artifactType, statusFields);
        }
    }
    if (isPromptPath(normalizedPath)) {
        return supported(normalizedPath, "prompt", contentFormat, true, sourceTemplate, scaffoldSource, artifactType, statusFields);
    }
    if (isWorkflowPath(normalizedPath)) {
        return supported(normalizedPath, "workflow", contentFormat, false, sourceTemplate, scaffoldSource, artifactType, statusFields);
    }
    if (isReferencePath(normalizedPath)) {
        return supported(normalizedPath, "reference", contentFormat, false, sourceTemplate, scaffoldSource, artifactType, statusFields);
    }
    if (contentFormat === "json" && normalizedPath.endsWith("/manifest.json")) {
        return unsupported(normalizedPath, contentFormat, artifactType, statusFields, "JSON manifest path is not a supported Day Shift prompt or template manifest.");
    }
    return unsupported(normalizedPath, contentFormat, artifactType, statusFields, statusFields.length > 0
        ? `Unsupported metadata family contains status-family fields: ${statusFields.join(", ")}.`
        : "Path and metadata do not match a supported Day Shift artifact family.");
}
function supported(artifactPath, family, contentFormat, metadataBearing, sourceTemplate, scaffoldSource, artifactType, statusFields) {
    return {
        path: artifactPath,
        family,
        supported: true,
        contentFormat,
        metadataBearing,
        sourceTemplate,
        scaffoldSource,
        artifactType,
        statusFields,
        reason: `Detected ${family} artifact family.`
    };
}
function unsupported(artifactPath, contentFormat, artifactType, statusFields, reason) {
    return {
        path: artifactPath,
        family: "unsupported",
        supported: false,
        contentFormat,
        metadataBearing: false,
        sourceTemplate: false,
        scaffoldSource: false,
        artifactType,
        statusFields,
        reason
    };
}
function familyFromArtifactType(artifactType) {
    if (PLANNING_ARTIFACT_TYPES.has(artifactType)) {
        return "planning";
    }
    if (artifactType === "implementation_summary") {
        return "implementation-summary";
    }
    if (artifactType === "reconciliation") {
        return "reconciliation";
    }
    if (artifactType === "prompt") {
        return "prompt";
    }
    if (artifactType === "template") {
        return "template";
    }
    return null;
}
function readArtifactType(content) {
    const parsed = (0, planning_document_utils_1.parseMarkdownFrontMatter)(content);
    if (!parsed.hasFrontMatter) {
        return null;
    }
    return (0, planning_document_utils_1.readTopLevelField)(parsed.frontMatterLines, "artifact_type")?.trim() || null;
}
function readStatusFields(content) {
    const parsed = (0, planning_document_utils_1.parseMarkdownFrontMatter)(content);
    if (!parsed.hasFrontMatter) {
        return [];
    }
    const fields = ["status", "readiness", "implementation_status", "reconciliation_status"];
    return fields.filter((field) => (0, planning_document_utils_1.readTopLevelField)(parsed.frontMatterLines, field) !== undefined);
}
function detectContentFormat(artifactPath) {
    const extension = node_path_1.default.extname(artifactPath).toLowerCase();
    if (extension === ".md") {
        return "markdown";
    }
    if (extension === ".json") {
        return "json";
    }
    return "unknown";
}
function isPromptPath(artifactPath) {
    return artifactPath.startsWith(".day-shift/prompts/") && artifactPath.endsWith(".md");
}
function isTemplateSourcePath(artifactPath) {
    return artifactPath.startsWith(".day-shift/templates/") && artifactPath.endsWith(".md");
}
function isWorkflowPath(artifactPath) {
    return artifactPath.startsWith(".day-shift/workflows/") && artifactPath.endsWith(".md");
}
function isReferencePath(artifactPath) {
    return artifactPath.startsWith(".day-shift/references/") && artifactPath.endsWith(".md");
}
function isScaffoldSourcePath(artifactPath) {
    return artifactPath.includes("/scaffolds/") || artifactPath.includes("/scaffold/");
}
function normalizeArtifactPath(artifactPath) {
    return artifactPath.replace(/\\/g, "/").replace(/^\.\//, "");
}
