"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 bordered_exports = {};
__export(bordered_exports, {
  formatTable: () => formatTable
});
module.exports = __toCommonJS(bordered_exports);
var import_yoctocolors_cjs = __toESM(require("../external/yoctocolors-cjs"));
var import_array = require("../primordials/array");
var import_math = require("../primordials/math");
var import_padding = require("./padding");
function formatTable(data, columns) {
  if (data.length === 0) {
    return "(no data)";
  }
  const widths = columns.map((col) => {
    const headerWidth = (0, import_padding.displayWidth)(col.header);
    const maxDataWidth = (0, import_math.MathMax)(
      ...data.map((row) => (0, import_padding.displayWidth)(String(row[col.key] ?? "")))
    );
    return col.width ?? (0, import_math.MathMax)(headerWidth, maxDataWidth);
  });
  const lines = [];
  const topBorder = `\u250C\u2500${widths.map((w) => "\u2500".repeat(w)).join("\u2500\u252C\u2500")}\u2500\u2510`;
  (0, import_array.ArrayPrototypePush)(lines, import_yoctocolors_cjs.default.dim(topBorder));
  const headerCells = columns.map((col, i) => {
    const text = import_yoctocolors_cjs.default.bold(col.header);
    return (0, import_padding.padText)(text, widths[i], col.align);
  });
  (0, import_array.ArrayPrototypePush)(
    lines,
    import_yoctocolors_cjs.default.dim("\u2502 ") + headerCells.join(import_yoctocolors_cjs.default.dim(" \u2502 ")) + import_yoctocolors_cjs.default.dim(" \u2502")
  );
  const headerSep = `\u251C\u2500${widths.map((w) => "\u2500".repeat(w)).join("\u2500\u253C\u2500")}\u2500\u2524`;
  (0, import_array.ArrayPrototypePush)(lines, import_yoctocolors_cjs.default.dim(headerSep));
  for (const row of data) {
    const cells = columns.map((col, i) => {
      let value = String(row[col.key] ?? "");
      if (col.color) {
        value = col.color(value);
      }
      return (0, import_padding.padText)(value, widths[i], col.align);
    });
    (0, import_array.ArrayPrototypePush)(
      lines,
      import_yoctocolors_cjs.default.dim("\u2502 ") + cells.join(import_yoctocolors_cjs.default.dim(" \u2502 ")) + import_yoctocolors_cjs.default.dim(" \u2502")
    );
  }
  const bottomBorder = `\u2514\u2500${widths.map((w) => "\u2500".repeat(w)).join("\u2500\u2534\u2500")}\u2500\u2518`;
  (0, import_array.ArrayPrototypePush)(lines, import_yoctocolors_cjs.default.dim(bottomBorder));
  return lines.join("\n");
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  formatTable
});
