"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatFileSourceUri = formatFileSourceUri;
exports.formatGithubDirectorySourceUri = formatGithubDirectorySourceUri;
exports.formatClaudePluginSourceUri = formatClaudePluginSourceUri;
exports.formatCodexPluginSourceUri = formatCodexPluginSourceUri;
exports.parseSkillSourceUri = parseSkillSourceUri;
const node_path_1 = __importDefault(require("node:path"));
const node_url_1 = require("node:url");
function formatFileSourceUri(filePath) {
    return (0, node_url_1.pathToFileURL)(node_path_1.default.resolve(filePath)).toString();
}
function formatGithubDirectorySourceUri(input) {
    const directoryPath = input.directoryPath === "" ? "." : input.directoryPath;
    return `github+https://github.com/${input.repo}.git#${encodeURIComponent(input.ref)}:${directoryPath}`;
}
function formatClaudePluginSourceUri(input) {
    return `claude-plugin://${encodeURIComponent(input.marketplace)}/${encodeURIComponent(input.plugin)}`;
}
function formatCodexPluginSourceUri(input) {
    const url = new URL(`codex-plugin://${encodeURIComponent(input.marketplace)}/${encodeURIComponent(input.plugin)}`);
    if (input.marketplacePath !== undefined && input.marketplacePath !== null) {
        url.searchParams.set("marketplacePath", input.marketplacePath);
    }
    return url.toString();
}
function parseSkillSourceUri(value) {
    if (value.startsWith("file://")) {
        return {
            kind: "file",
            path: (0, node_url_1.fileURLToPath)(value),
        };
    }
    if (value.startsWith("github+https://")) {
        return parseGithubDirectorySourceUri(value);
    }
    if (value.startsWith("claude-plugin://")) {
        return parseClaudePluginSourceUri(value);
    }
    if (value.startsWith("codex-plugin://")) {
        return parseCodexPluginSourceUri(value);
    }
    throw new Error(`Unsupported skill source URI: ${value}`);
}
function parseGithubDirectorySourceUri(value) {
    const withoutScheme = value.slice("github+".length);
    const hashIndex = withoutScheme.indexOf("#");
    if (hashIndex < 0) {
        throw new Error(`Invalid GitHub skill source URI: ${value}`);
    }
    const cloneUrl = withoutScheme.slice(0, hashIndex);
    const fragment = withoutScheme.slice(hashIndex + 1);
    const separatorIndex = fragment.indexOf(":");
    if (separatorIndex <= 0 || separatorIndex === fragment.length - 1) {
        throw new Error(`Invalid GitHub skill source URI: ${value}`);
    }
    const ref = decodeURIComponent(fragment.slice(0, separatorIndex));
    const directoryPath = fragment.slice(separatorIndex + 1);
    const match = /^https:\/\/github\.com\/([^/]+)\/([^/]+?)(?:\.git)?$/u.exec(cloneUrl);
    if (match === null || ref.trim() === "" || directoryPath.trim() === "") {
        throw new Error(`Invalid GitHub skill source URI: ${value}`);
    }
    const owner = decodeURIComponent(match[1] ?? "");
    const repo = decodeURIComponent(match[2] ?? "");
    if (owner === "" ||
        repo === "" ||
        !isSafeGithubDirectoryPath(directoryPath)) {
        throw new Error(`Invalid GitHub skill source URI: ${value}`);
    }
    return {
        kind: "github",
        owner,
        repo,
        ref,
        directoryPath,
    };
}
function parseClaudePluginSourceUri(value) {
    const parsed = new URL(value);
    const marketplace = decodeURIComponent(parsed.hostname);
    const plugin = decodeURIComponent(parsed.pathname.replace(/^\/+/u, ""));
    if (!isValidSourceSegment(marketplace) || !isValidSourceSegment(plugin)) {
        throw new Error(`Invalid Claude plugin skill source URI: ${value}`);
    }
    return {
        kind: "claude-plugin",
        marketplace,
        plugin,
    };
}
function parseCodexPluginSourceUri(value) {
    const parsed = new URL(value);
    const marketplace = decodeURIComponent(parsed.hostname);
    const plugin = decodeURIComponent(parsed.pathname.replace(/^\/+/u, ""));
    const marketplacePath = parsed.searchParams.get("marketplacePath");
    if (!isValidSourceSegment(marketplace) ||
        !isValidSourceSegment(plugin) ||
        (marketplacePath !== null && !node_path_1.default.isAbsolute(marketplacePath))) {
        throw new Error(`Invalid Codex plugin skill source URI: ${value}`);
    }
    return {
        kind: "codex-plugin",
        marketplace,
        plugin,
        marketplacePath,
    };
}
function isSafeGithubDirectoryPath(value) {
    if (value === ".") {
        return true;
    }
    const segments = value.split("/");
    return (!value.startsWith("/") &&
        segments.length > 0 &&
        segments.every((segment) => segment !== "" && segment !== "." && segment !== ".."));
}
function isValidSourceSegment(value) {
    return /^(?=.{1,63}$)[a-z0-9]+(?:-[a-z0-9]+)*$/u.test(value);
}
