"use strict";
/* 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 code_exports = {};
__export(code_exports, {
  getCodeCoverage: () => getCodeCoverage
});
module.exports = __toCommonJS(code_exports);
var import_fs = require("fs");
var import_fs2 = require("../fs");
var import_objects = require("../objects");
var import_spawn = require("../spawn");
let _path;
// @__NO_SIDE_EFFECTS__
function getPath() {
  if (_path === void 0) {
    _path = require("node:path");
  }
  return _path;
}
async function getCodeCoverage(options) {
  const path = /* @__PURE__ */ getPath();
  const opts = {
    __proto__: null,
    coveragePath: path.join(process.cwd(), "coverage/coverage-final.json"),
    generateIfMissing: false,
    ...options
  };
  const { coveragePath, generateIfMissing } = opts;
  if (!coveragePath) {
    throw new Error("Coverage path is required");
  }
  const coverageExists = await import_fs.promises.access(coveragePath).then(() => true).catch(() => false);
  if (!coverageExists) {
    if (generateIfMissing) {
      await (0, import_spawn.spawn)("vitest", ["run", "--coverage"], {
        cwd: process.cwd(),
        stdio: "inherit"
      });
    } else {
      throw new Error(
        `Coverage file not found at "${coveragePath}". Run tests with coverage first.`
      );
    }
  }
  const coverageData = await (0, import_fs2.readJson)(coveragePath);
  if (!(0, import_objects.isObjectObject)(coverageData)) {
    throw new Error(`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 Object.values(v8Data)) {
    if (!(0, import_objects.isObjectObject)(fileCoverage)) {
      continue;
    }
    const fc = fileCoverage;
    if (fc.s && (0, import_objects.isObjectObject)(fc.s)) {
      const statementCounts = Object.values(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_objects.isObjectObject)(fc.b)) {
      const branchCounts = Object.values(fc.b);
      for (const branches of branchCounts) {
        if (Array.isArray(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_objects.isObjectObject)(fc.f)) {
      const functionCounts = Object.values(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)
  };
}
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
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  getCodeCoverage
});
