"use client";
import { useCallback, useMemo } from "react";
import { parse as parseYaml } from "yaml";
import { toast } from "../feedback/toast";
import { serializeAgentYaml, serializeMcpServerYaml } from "./serialize-resource-yaml";
import { serializeWorkflowYaml } from "../workflow/serialize-workflow-yaml";
// ---------------------------------------------------------------------------
// Hook
// ---------------------------------------------------------------------------
/**
 * Headless export hook for Stigmer resources (Agent, McpServer).
 *
 * Serializes the resource into YAML and JSON formats and provides
 * stable callbacks for copying to clipboard or triggering a file
 * download. All operations are client-side — no additional API calls.
 *
 * The serialized output uses the canonical Stigmer resource format
 * (`apiVersion`, `kind`, `metadata`, `spec`) with snake_case field
 * names, identical to what the CLI produces and what `parseResourceYaml`
 * accepts — ensuring full round-trip compatibility.
 *
 * @param options - Resource kind and the proto resource object.
 * @returns Stable callbacks and memoized serialized strings.
 *
 * @example
 * ```tsx
 * const { agent } = useAgent(org, slug);
 * const { copyYaml, downloadYaml } = useExportResource({
 *   kind: "Agent",
 *   resource: agent,
 * });
 *
 * <ActionMenu.Item onSelect={copyYaml}>Export YAML</ActionMenu.Item>
 * <ActionMenu.Item onSelect={downloadYaml}>Download YAML</ActionMenu.Item>
 * ```
 *
 * @see {@link serializeAgentYaml} for the underlying Agent serializer
 * @see {@link serializeMcpServerYaml} for the underlying McpServer serializer
 */
export function useExportResource({ kind, resource, }) {
    const yaml = useMemo(() => {
        if (!resource)
            return null;
        if (kind === "Agent")
            return serializeAgentYaml(resource);
        if (kind === "Workflow")
            return serializeWorkflowYaml(resource);
        return serializeMcpServerYaml(resource);
    }, [kind, resource]);
    const json = useMemo(() => {
        if (!resource)
            return null;
        return serializeResourceJson(resource, kind);
    }, [kind, resource]);
    const slug = useMemo(() => {
        if (!resource)
            return "resource";
        return resource.metadata?.slug || resource.metadata?.name || "resource";
    }, [resource]);
    const copyYaml = useCallback(async () => {
        if (!yaml)
            return;
        await copyToClipboard(yaml);
        toast.success("YAML copied to clipboard");
    }, [yaml]);
    const copyJson = useCallback(async () => {
        if (!json)
            return;
        await copyToClipboard(json);
        toast.success("JSON copied to clipboard");
    }, [json]);
    const downloadYaml = useCallback(() => {
        if (!yaml)
            return;
        downloadFile(yaml, `${slug}.yaml`, "text/yaml");
    }, [yaml, slug]);
    const downloadJson = useCallback(() => {
        if (!json)
            return;
        downloadFile(json, `${slug}.json`, "application/json");
    }, [json, slug]);
    return useMemo(() => ({ copyYaml, copyJson, downloadYaml, downloadJson, yaml, json }), [copyYaml, copyJson, downloadYaml, downloadJson, yaml, json]);
}
// ---------------------------------------------------------------------------
// JSON serialization
// ---------------------------------------------------------------------------
/**
 * Produces a JSON representation of the resource using the same canonical
 * structure as the YAML export (apiVersion, kind, metadata, spec).
 *
 * Internally parses the YAML output to get a clean plain object, then
 * re-stringifies as JSON. This guarantees the JSON structure is identical
 * to the YAML structure (snake_case fields, no status).
 */
function serializeResourceJson(resource, kind) {
    let yamlStr;
    if (kind === "Agent")
        yamlStr = serializeAgentYaml(resource);
    else if (kind === "Workflow")
        yamlStr = serializeWorkflowYaml(resource);
    else
        yamlStr = serializeMcpServerYaml(resource);
    const doc = parseYaml(yamlStr);
    return JSON.stringify(doc, null, 2);
}
// ---------------------------------------------------------------------------
// Clipboard
// ---------------------------------------------------------------------------
async function copyToClipboard(text) {
    try {
        await navigator.clipboard.writeText(text);
    }
    catch {
        fallbackCopy(text);
    }
}
function fallbackCopy(text) {
    const textarea = document.createElement("textarea");
    textarea.value = text;
    textarea.style.position = "fixed";
    textarea.style.opacity = "0";
    document.body.appendChild(textarea);
    textarea.select();
    document.execCommand("copy");
    document.body.removeChild(textarea);
}
// ---------------------------------------------------------------------------
// File download
// ---------------------------------------------------------------------------
function downloadFile(content, filename, mimeType) {
    const blob = new Blob([content], { type: `${mimeType};charset=utf-8;` });
    const url = URL.createObjectURL(blob);
    const link = document.createElement("a");
    link.href = url;
    link.download = filename;
    link.style.display = "none";
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    URL.revokeObjectURL(url);
}
//# sourceMappingURL=useExportResource.js.map