"use strict";
/* Socket Lib - Built with esbuild */
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
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_sentinels = require("../constants/sentinels");
var import_licenses = require("../constants/licenses");
var import_spdx_correct = __toESM(require("../external/spdx-correct"));
var import_spdx_expression_parse = __toESM(require("../external/spdx-expression-parse"));
var import_predicates = require("../objects/predicates");
var import_normalize = require("../paths/normalize");
var import_error = require("../primordials/error");
var import_map_set = require("../primordials/map-set");
var import_regexp = require("../primordials/regexp");
var import_path = require("../node/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 (.+)$/;
// @__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 = new import_map_set.MapCtor();
  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_predicates.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) {
  try {
    return (0, import_spdx_expression_parse.default)(spdxExp);
  } catch {
  }
  const corrected = (0, import_spdx_correct.default)(spdxExp);
  return corrected ? (0, import_spdx_expression_parse.default)(corrected) : void 0;
}
// @__NO_SIDE_EFFECTS__
function resolvePackageLicenses(licenseFieldValue, where) {
  if (licenseFieldValue === "UNLICENSED" || licenseFieldValue === "UNLICENCED") {
    return [{ license: "UNLICENSED" }];
  }
  const match = (0, import_regexp.RegExpPrototypeExec)(fileReferenceRegExp, licenseFieldValue);
  if (match) {
    const path = (0, import_path.getNodePath)();
    return [
      {
        license: licenseFieldValue,
        inFile: (0, import_normalize.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_sentinels.LOOP_SENTINEL) {
      throw new import_error.ErrorCtor(
        "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_predicates.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
});
