"use strict";
/* Socket Lib - Built with esbuild */
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var manifest_exports = {};
__export(manifest_exports, {
  getTool: () => getTool,
  getToolFlavor: () => getToolFlavor,
  isObject: () => isObject,
  parseChecksum: () => parseChecksum,
  parseChecksums: () => parseChecksums,
  parseToolEntry: () => parseToolEntry,
  readExternalToolsManifest: () => readExternalToolsManifest,
  tryParseFlavored: () => tryParseFlavored
});
module.exports = __toCommonJS(manifest_exports);
var import_read_json = require("../fs/read-json");
var import_integrity = require("../integrity");
var import_array = require("../primordials/array");
var import_error = require("../primordials/error");
function getTool(manifest, toolName) {
  const entry = manifest.tools[toolName];
  return entry?.kind === "tool" ? entry.entry : void 0;
}
function getToolFlavor(manifest, toolName, flavor) {
  const entry = manifest.tools[toolName];
  if (entry?.kind !== "flavored") {
    return void 0;
  }
  return entry.entry.flavors[flavor];
}
function isObject(value) {
  return typeof value === "object" && value !== null && !(0, import_array.ArrayIsArray)(value);
}
function parseChecksum(raw, toolName, platformKey) {
  if (!isObject(raw)) {
    throw new import_error.ErrorCtor(
      `external-tools.json: tool '${toolName}' checksum entry '${platformKey}' must be an object, got: ${typeof raw}`
    );
  }
  const r = raw;
  if (typeof r.asset !== "string" || r.asset.length === 0) {
    throw new import_error.ErrorCtor(
      `external-tools.json: tool '${toolName}' platform '${platformKey}' is missing a non-empty 'asset' string`
    );
  }
  if (typeof r.integrity !== "string" || !(0, import_integrity.isIntegrityString)(r.integrity)) {
    throw new import_error.ErrorCtor(
      `external-tools.json: tool '${toolName}' platform '${platformKey}' has an invalid 'integrity' (expected SRI sha512 or sha256 form): ${r.integrity}`
    );
  }
  return { asset: r.asset, integrity: r.integrity };
}
function parseChecksums(raw, toolName) {
  if (!isObject(raw)) {
    throw new import_error.ErrorCtor(
      `external-tools.json: tool '${toolName}' is missing a 'checksums' object`
    );
  }
  const out = {};
  for (const platformKey of Object.keys(raw)) {
    out[platformKey] = parseChecksum(raw[platformKey], toolName, platformKey);
  }
  return out;
}
function parseToolEntry(raw, toolName) {
  if (!isObject(raw)) {
    return { kind: "other", raw };
  }
  const r = raw;
  if (isObject(r.checksums)) {
    if (typeof r.description !== "string" || typeof r.version !== "string" || typeof r.release !== "string" || typeof r.repository !== "string") {
      return { kind: "other", raw };
    }
    return {
      kind: "tool",
      entry: {
        description: r.description,
        version: r.version,
        release: r.release,
        repository: r.repository,
        binaryName: typeof r.binaryName === "string" ? r.binaryName : void 0,
        notes: (0, import_array.ArrayIsArray)(r.notes) ? r.notes : void 0,
        checksums: parseChecksums(r.checksums, toolName)
      }
    };
  }
  const flavored = tryParseFlavored(raw, toolName);
  if (flavored) {
    return { kind: "flavored", entry: flavored };
  }
  return { kind: "other", raw };
}
async function readExternalToolsManifest(filepath) {
  const raw = await (0, import_read_json.readJson)(filepath);
  if (!isObject(raw)) {
    throw new import_error.ErrorCtor(
      `external-tools.json: expected top-level object, got: ${typeof raw}`
    );
  }
  const tools = {};
  for (const toolName of Object.keys(raw)) {
    if (toolName.startsWith("$")) {
      continue;
    }
    tools[toolName] = parseToolEntry(raw[toolName], toolName);
  }
  return { tools };
}
function tryParseFlavored(raw, toolName) {
  const flavors = {};
  for (const key of Object.keys(raw)) {
    const value = raw[key];
    if (!isObject(value)) {
      continue;
    }
    const rf = value;
    if (!isObject(rf.checksums)) {
      continue;
    }
    if (typeof rf.repository !== "string") {
      continue;
    }
    flavors[key] = {
      repository: rf.repository,
      binaryName: typeof rf.binaryName === "string" ? rf.binaryName : void 0,
      checksums: parseChecksums(rf.checksums, `${toolName}.${key}`)
    };
  }
  if (Object.keys(flavors).length === 0) {
    return void 0;
  }
  return {
    description: String(raw["description"] ?? ""),
    version: String(raw["version"] ?? ""),
    release: String(raw["release"] ?? ""),
    notes: (0, import_array.ArrayIsArray)(raw["notes"]) ? raw["notes"] : void 0,
    flavors
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  getTool,
  getToolFlavor,
  isObject,
  parseChecksum,
  parseChecksums,
  parseToolEntry,
  readExternalToolsManifest,
  tryParseFlavored
});
