/* Socket Lib - Built with esbuild */
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 licenses_exports = {};
__export(licenses_exports, {
  collectIncompatibleLicenses: () => collectIncompatibleLicenses,
  collectLicenseWarnings: () => collectLicenseWarnings,
  createAstNode: () => createAstNode,
  createBinaryOperationNode: () => createBinaryOperationNode,
  createLicenseNode: () => createLicenseNode,
  parseSpdxExp: () => parseSpdxExp,
  resolvePackageLicenses: () => resolvePackageLicenses,
  visitLicenses: () => visitLicenses
});
module.exports = __toCommonJS(licenses_exports);
var import_core = require("#constants/core");
var import_licenses = require("#constants/licenses");
var import_objects = require("../objects");
var import_path = require("../path");
const copyLeftLicenses = (0, import_licenses.getCopyLeftLicenses)();
const BINARY_OPERATION_NODE_TYPE = "BinaryOperation";
const LICENSE_NODE_TYPE = "License";
const fileReferenceRegExp = /^SEE LICEN[CS]E IN (.+)$/;
let _path;
// @__NO_SIDE_EFFECTS__
function getPath() {
  if (_path === void 0) {
    _path = require("node:path");
  }
  return _path;
}
let _spdxCorrect;
// @__NO_SIDE_EFFECTS__
function getSpdxCorrect() {
  if (_spdxCorrect === void 0) {
    _spdxCorrect = require("../external/spdx-correct");
  }
  return _spdxCorrect;
}
let _spdxExpParse;
// @__NO_SIDE_EFFECTS__
function getSpdxExpParse() {
  if (_spdxExpParse === void 0) {
    _spdxExpParse = require("../external/spdx-expression-parse");
  }
  return _spdxExpParse;
}
// @__NO_SIDE_EFFECTS__
function collectIncompatibleLicenses(licenseNodes) {
  const result = [];
  for (let i = 0, { length } = licenseNodes; i < length; i += 1) {
    const node = licenseNodes[i];
    if (node && copyLeftLicenses.has(node.license)) {
      result.push(node);
    }
  }
  return result;
}
// @__NO_SIDE_EFFECTS__
function collectLicenseWarnings(licenseNodes) {
  const warnings = /* @__PURE__ */ new Map();
  for (let i = 0, { length } = licenseNodes; i < length; i += 1) {
    const node = licenseNodes[i];
    if (!node) {
      continue;
    }
    const { license } = node;
    if (license === "UNLICENSED") {
      warnings.set("UNLICENSED", "Package is unlicensed");
    } else if (node.inFile !== void 0) {
      warnings.set("IN_FILE", `License terms specified in ${node.inFile}`);
    }
  }
  return [...warnings.values()];
}
// @__NO_SIDE_EFFECTS__
function createAstNode(rawNode) {
  return (0, import_objects.hasOwn)(rawNode, "license") ? /* @__PURE__ */ createLicenseNode(rawNode) : /* @__PURE__ */ createBinaryOperationNode(rawNode);
}
// @__NO_SIDE_EFFECTS__
function createBinaryOperationNode(rawNodeParam) {
  let left;
  let right;
  let rawLeft = rawNodeParam.left;
  let rawRight = rawNodeParam.right;
  const { conjunction } = rawNodeParam;
  return {
    __proto__: null,
    type: BINARY_OPERATION_NODE_TYPE,
    get left() {
      if (left === void 0) {
        left = /* @__PURE__ */ createAstNode(rawLeft);
        rawLeft = void 0;
      }
      return left;
    },
    conjunction,
    get right() {
      if (right === void 0) {
        right = /* @__PURE__ */ createAstNode(rawRight);
        rawRight = void 0;
      }
      return right;
    }
  };
}
// @__NO_SIDE_EFFECTS__
function createLicenseNode(rawNode) {
  return {
    __proto__: null,
    ...rawNode,
    type: LICENSE_NODE_TYPE
  };
}
// @__NO_SIDE_EFFECTS__
function parseSpdxExp(spdxExp) {
  const spdxExpParse = /* @__PURE__ */ getSpdxExpParse();
  try {
    return spdxExpParse(spdxExp);
  } catch {
  }
  const spdxCorrect = /* @__PURE__ */ getSpdxCorrect();
  const corrected = spdxCorrect(spdxExp);
  return corrected ? spdxExpParse(corrected) : void 0;
}
// @__NO_SIDE_EFFECTS__
function resolvePackageLicenses(licenseFieldValue, where) {
  if (licenseFieldValue === "UNLICENSED" || licenseFieldValue === "UNLICENCED") {
    return [{ license: "UNLICENSED" }];
  }
  const match = fileReferenceRegExp.exec(licenseFieldValue);
  if (match) {
    const path = /* @__PURE__ */ getPath();
    return [
      {
        license: licenseFieldValue,
        inFile: (0, import_path.normalizePath)(path.relative(where, match[1] || ""))
      }
    ];
  }
  const licenseNodes = [];
  const ast = /* @__PURE__ */ parseSpdxExp(licenseFieldValue);
  if (ast) {
    /* @__PURE__ */ visitLicenses(ast, {
      License(node) {
        const { license } = node;
        if (license.startsWith("LicenseRef") || license.startsWith("DocumentRef")) {
          licenseNodes.length = 0;
          return false;
        }
        licenseNodes.push(node);
      }
    });
  }
  return licenseNodes;
}
// @__NO_SIDE_EFFECTS__
function visitLicenses(ast, visitor) {
  const queue = [
    [/* @__PURE__ */ createAstNode(ast), void 0]
  ];
  let pos = 0;
  let { length: queueLength } = queue;
  while (pos < queueLength) {
    if (pos === import_core.LOOP_SENTINEL) {
      throw new Error("Detected infinite loop in ast crawl of visitLicenses");
    }
    const { 0: node, 1: parent } = queue[pos++];
    const { type } = node;
    const visitorRecord = visitor;
    if (typeof visitorRecord[type] === "function" && (0, import_objects.hasOwn)(visitor, type)) {
      if (type === LICENSE_NODE_TYPE) {
        const licenseVisitor = visitorRecord["License"];
        if (typeof licenseVisitor === "function" && licenseVisitor(node, parent) === false) {
          break;
        }
      } else if (type === BINARY_OPERATION_NODE_TYPE) {
        const binaryOpVisitor = visitorRecord["BinaryOperation"];
        if (typeof binaryOpVisitor === "function" && binaryOpVisitor(node, parent) === false) {
          break;
        }
      }
    }
    if (type === BINARY_OPERATION_NODE_TYPE) {
      queue[queueLength++] = [node.left, node];
      queue[queueLength++] = [node.right, node];
    }
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  collectIncompatibleLicenses,
  collectLicenseWarnings,
  createAstNode,
  createBinaryOperationNode,
  createLicenseNode,
  parseSpdxExp,
  resolvePackageLicenses,
  visitLicenses
});
//# sourceMappingURL=licenses.js.map
