"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 code_exports = {};
__export(code_exports, {
  calculateMetric: () => calculateMetric,
  getCodeCoverage: () => getCodeCoverage
});
module.exports = __toCommonJS(code_exports);
var import_node_process = __toESM(require("node:process"));
var import_read_json = require("../fs/read-json");
var import_predicates = require("../objects/predicates");
var import_spawn = require("../spawn/spawn");
var import_array = require("../primordials/array");
var import_error = require("../primordials/error");
var import_object = require("../primordials/object");
var import_fs = require("../node/fs");
var import_path = require("../node/path");
function calculateMetric(data) {
  const percent = data.total === 0 ? "0.00" : (data.covered / data.total * 100).toFixed(2);
  return {
    covered: data.covered,
    percent,
    total: data.total
  };
}
async function getCodeCoverage(options) {
  const path = (0, import_path.getNodePath)();
  const opts = {
    __proto__: null,
    coveragePath: path.join(import_node_process.default.cwd(), "coverage/coverage-final.json"),
    generateIfMissing: false,
    ...options
  };
  const { coveragePath, generateIfMissing } = opts;
  if (!coveragePath) {
    throw new import_error.ErrorCtor("Coverage path is required");
  }
  const fs = (0, import_fs.getNodeFs)();
  if (!fs.existsSync(coveragePath)) {
    if (generateIfMissing) {
      await (0, import_spawn.spawn)("vitest", ["run", "--coverage"], {
        cwd: import_node_process.default.cwd(),
        stdio: "inherit"
      });
    } else {
      throw new import_error.ErrorCtor(
        `Coverage file not found at "${coveragePath}". Run tests with coverage first.`
      );
    }
  }
  const coverageData = await (0, import_read_json.readJson)(coveragePath);
  if (!(0, import_predicates.isPlainObject)(coverageData)) {
    throw new import_error.ErrorCtor(`Invalid coverage data format in "${coveragePath}"`);
  }
  const totals = {
    __proto__: null,
    branches: { __proto__: null, covered: 0, total: 0 },
    functions: { __proto__: null, covered: 0, total: 0 },
    lines: { __proto__: null, covered: 0, total: 0 },
    statements: { __proto__: null, covered: 0, total: 0 }
  };
  const v8Data = coverageData;
  for (const fileCoverage of (0, import_object.ObjectValues)(v8Data)) {
    if (!(0, import_predicates.isPlainObject)(fileCoverage)) {
      continue;
    }
    const fc = fileCoverage;
    if (fc.s && (0, import_predicates.isPlainObject)(fc.s)) {
      const statementCounts = (0, import_object.ObjectValues)(fc.s);
      for (const count of statementCounts) {
        if (typeof count === "number") {
          totals.statements.total += 1;
          if (count > 0) {
            totals.statements.covered += 1;
          }
        }
      }
    }
    if (fc.b && (0, import_predicates.isPlainObject)(fc.b)) {
      const branchCounts = (0, import_object.ObjectValues)(fc.b);
      for (const branches of branchCounts) {
        if ((0, import_array.ArrayIsArray)(branches)) {
          for (const count of branches) {
            if (typeof count === "number") {
              totals.branches.total += 1;
              if (count > 0) {
                totals.branches.covered += 1;
              }
            }
          }
        }
      }
    }
    if (fc.f && (0, import_predicates.isPlainObject)(fc.f)) {
      const functionCounts = (0, import_object.ObjectValues)(fc.f);
      for (const count of functionCounts) {
        if (typeof count === "number") {
          totals.functions.total += 1;
          if (count > 0) {
            totals.functions.covered += 1;
          }
        }
      }
    }
    totals.lines.covered = totals.statements.covered;
    totals.lines.total = totals.statements.total;
  }
  return {
    branches: calculateMetric(totals.branches),
    functions: calculateMetric(totals.functions),
    lines: calculateMetric(totals.lines),
    statements: calculateMetric(totals.statements)
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  calculateMetric,
  getCodeCoverage
});
