import { parse as parseYaml } from "yaml";
const NOT_DETECTED = { detected: false };
const KIND_DISPLAY_NAMES = {
    Agent: "Agent",
    McpServer: "MCP Server",
};
const KNOWN_KINDS = new Set(Object.keys(KIND_DISPLAY_NAMES));
const STIGMER_API_VERSION_PATTERN = /^[a-z]+\.stigmer\.ai\/v\d+$/;
function isPlainObject(value) {
    return typeof value === "object" && value !== null && !Array.isArray(value);
}
/**
 * Detects whether a YAML content string represents a Stigmer platform resource.
 *
 * Performs a best-effort structural check against the Stigmer resource
 * convention: a YAML document with `apiVersion` matching `*.stigmer.ai/*`,
 * a recognized `kind`, and `metadata.name`.
 *
 * **Resilient by design** — any parse failure, missing field, or unexpected
 * structure returns `{ detected: false }`. This function never throws.
 *
 * Only the first YAML document is inspected when the content contains
 * multiple documents separated by `---`.
 *
 * @param content - Raw YAML content string (typically from {@link useArtifactContent}).
 * @returns A discriminated union: `{ detected: true, kind, ... }` when a
 *   recognized Stigmer resource is found, `{ detected: false }` otherwise.
 *
 * @example
 * ```ts
 * const result = detectStigmerResource(yamlString);
 * if (result.detected) {
 *   console.log(`Found ${result.displayName}: ${result.resourceName}`);
 * }
 * ```
 *
 * @see {@link useDetectStigmerResource} for the React hook wrapper
 * @see {@link StigmerResourceKind} for recognized resource kinds
 */
export function detectStigmerResource(content) {
    let parsed;
    try {
        parsed = parseYaml(content);
    }
    catch {
        return NOT_DETECTED;
    }
    if (!isPlainObject(parsed)) {
        return NOT_DETECTED;
    }
    const { apiVersion, kind, metadata } = parsed;
    if (typeof apiVersion !== "string" || !STIGMER_API_VERSION_PATTERN.test(apiVersion)) {
        return NOT_DETECTED;
    }
    if (typeof kind !== "string" || !KNOWN_KINDS.has(kind)) {
        return NOT_DETECTED;
    }
    if (!isPlainObject(metadata)) {
        return NOT_DETECTED;
    }
    const name = metadata.name;
    if (typeof name !== "string" || name.length === 0) {
        return NOT_DETECTED;
    }
    const resourceKind = kind;
    return {
        detected: true,
        apiVersion,
        kind: resourceKind,
        displayName: KIND_DISPLAY_NAMES[resourceKind],
        resourceName: name,
        resourceOrg: typeof metadata.org === "string" && metadata.org.length > 0
            ? metadata.org
            : undefined,
    };
}
//# sourceMappingURL=detect-stigmer-resource.js.map