"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractTextFiles = extractTextFiles;
exports.shouldCollapsePaste = shouldCollapsePaste;
exports.extensionForLabel = extensionForLabel;
exports.detectLanguage = detectLanguage;
const FILE_TAG_RE = /<file\s+path="([^"]+)">\n([\s\S]*?)\n<\/file>/g;
function extractTextFiles(text) {
    const textFiles = [];
    const cleaned = text
        .replace(FILE_TAG_RE, (_match, name, content) => {
        textFiles.push({ name, content });
        return "";
    })
        .replace(/\n{3,}/g, "\n\n")
        .trim();
    return { cleaned, textFiles };
}
const MIN_LINES = 10;
// Map Magika content type labels to file extensions for naming and syntax highlighting.
const LABEL_TO_EXT = {
    asm: "asm",
    awk: "awk",
    batch: "bat",
    c: "c",
    clojure: "clj",
    cmake: "cmake",
    cobol: "cob",
    coffeescript: "coffee",
    cpp: "cpp",
    cs: "cs",
    css: "css",
    csv: "csv",
    dart: "dart",
    diff: "diff",
    dockerfile: "dockerfile",
    elixir: "ex",
    erlang: "erl",
    fortran: "f90",
    gleam: "gleam",
    go: "go",
    gradle: "gradle",
    groovy: "groovy",
    h: "h",
    handlebars: "hbs",
    haskell: "hs",
    hcl: "tf",
    hpp: "hpp",
    html: "html",
    ini: "ini",
    java: "java",
    javascript: "js",
    json: "json",
    jsonc: "jsonc",
    jsonl: "jsonl",
    jsx: "jsx",
    julia: "jl",
    kotlin: "kt",
    latex: "tex",
    less: "less",
    lisp: "lisp",
    lua: "lua",
    makefile: "makefile",
    markdown: "md",
    matlab: "m",
    nim: "nim",
    objectivec: "m",
    ocaml: "ml",
    odin: "odin",
    pascal: "pas",
    perl: "pl",
    php: "php",
    powershell: "ps1",
    prolog: "pl",
    proto: "proto",
    python: "py",
    r: "r",
    rst: "rst",
    ruby: "rb",
    rust: "rs",
    scala: "scala",
    scheme: "scm",
    scss: "scss",
    shell: "sh",
    solidity: "sol",
    sql: "sql",
    svelte: "svelte",
    svg: "svg",
    swift: "swift",
    tcl: "tcl",
    toml: "toml",
    tsx: "tsx",
    typescript: "ts",
    vba: "vba",
    verilog: "v",
    vhdl: "vhdl",
    vue: "vue",
    xml: "xml",
    yaml: "yaml",
    yara: "yar",
    zig: "zig",
};
function shouldCollapsePaste(content) {
    return content.split(/\r?\n/).length >= MIN_LINES;
}
function extensionForLabel(label) {
    return LABEL_TO_EXT[label];
}
// Lazy-loaded client-side Magika instance
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let magikaPromise = null;
async function detectLanguage(text) {
    try {
        if (!magikaPromise) {
            magikaPromise = Promise.resolve().then(() => __importStar(require("magika"))).then((m) => m.Magika.create());
        }
        const magika = await magikaPromise;
        const bytes = new TextEncoder().encode(text.slice(0, 8192));
        const result = await magika.identifyBytes(bytes);
        const label = result.prediction.output.label;
        if (label === "txt" || label === "unknown" || label === "empty")
            return null;
        return LABEL_TO_EXT[label] ? label : null;
    }
    catch {
        return null;
    }
}
