"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 parse_exports = {};
__export(parse_exports, {
  commonParseArgsConfig: () => commonParseArgsConfig,
  getPositionalArgs: () => getPositionalArgs,
  hasFlag: () => hasFlag,
  parseArgs: () => parseArgs,
  parseArgsWithDefaults: () => parseArgsWithDefaults
});
module.exports = __toCommonJS(parse_exports);
var import_node_process = __toESM(require("node:process"));
var import_yargs_parser = __toESM(require("../external/yargs-parser"));
var import_array = require("../primordials/array");
var import_object = require("../primordials/object");
var import_string = require("../primordials/string");
const commonParseArgsConfig = {
  options: {
    force: {
      type: "boolean",
      short: "f",
      default: false
    },
    quiet: {
      type: "boolean",
      short: "q",
      default: false
    }
  },
  strict: false
};
function getPositionalArgs(startIndex = 2) {
  const args = import_node_process.default.argv.slice(startIndex);
  const positionals = [];
  let i = 0;
  while (i < args.length) {
    const arg = args[i];
    if ((0, import_string.StringPrototypeStartsWith)(arg, "-")) {
      break;
    }
    positionals.push(arg);
    i++;
  }
  return positionals;
}
function hasFlag(flag, argv = import_node_process.default.argv) {
  return (0, import_array.ArrayPrototypeIncludes)(argv, `--${flag}`);
}
function parseArgs(config = {}) {
  const {
    allowNegative = false,
    allowPositionals = true,
    args = import_node_process.default.argv.slice(2),
    configuration,
    options = {},
    strict = true
  } = config;
  const yargsOptions = {
    // Arrays of option names to treat as specific types.
    boolean: [],
    string: [],
    array: [],
    // Maps for aliases, defaults, and transformations.
    alias: {},
    default: {},
    coerce: {},
    "unknown-options-as-args": !strict,
    "parse-numbers": false,
    "parse-positional-numbers": false,
    "boolean-negation": !allowNegative,
    "halt-at-non-option": !allowPositionals,
    configuration: {
      // Enable kebab-case to camelCase conversion (e.g., --temp-dir → tempDir).
      "camel-case-expansion": true,
      // Disable dot notation to avoid confusing nested property parsing.
      "dot-notation": false,
      // Convert duplicate arguments into arrays automatically.
      "duplicate-arguments-array": true,
      // Flatten nested arrays from duplicate arguments for cleaner output.
      "flatten-duplicate-arrays": true,
      // Populate the '--' key with arguments after the -- separator.
      "populate--": true,
      // Allow short option grouping like -abc for -a -b -c.
      "short-option-groups": true,
      // Keep aliased keys in the result for flexibility.
      "strip-aliased": false,
      // Keep both kebab-case and camelCase keys for flexibility.
      "strip-dashed": false,
      ...configuration
    }
  };
  for (const { 0: key, 1: optionConfig } of (0, import_object.ObjectEntries)(options)) {
    const {
      coerce,
      default: defaultValue,
      multiple,
      short,
      type
    } = optionConfig;
    if (type === "boolean") {
      yargsOptions.boolean?.push(key);
    } else if (type === "string") {
      yargsOptions.string?.push(key);
    }
    if (multiple) {
      yargsOptions.array?.push(key);
    }
    if (short) {
      ;
      yargsOptions.alias[short] = key;
    }
    if (defaultValue !== void 0) {
      ;
      yargsOptions.default[key] = defaultValue;
    }
    if (coerce) {
      ;
      yargsOptions.coerce[key] = coerce;
    }
  }
  const parsed = (0, import_yargs_parser.default)(args, yargsOptions);
  const positionals = parsed._ || [];
  const { _, ...values } = parsed;
  const stringPositionals = positionals.map(String);
  return {
    values,
    positionals: stringPositionals,
    raw: parsed
  };
}
function parseArgsWithDefaults(config = {}) {
  return parseArgs({
    strict: false,
    allowPositionals: true,
    ...config
  });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  commonParseArgsConfig,
  getPositionalArgs,
  hasFlag,
  parseArgs,
  parseArgsWithDefaults
});
