"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : /* @__PURE__ */ Symbol.for("Symbol." + name);
var __typeError = (msg) => {
  throw TypeError(msg);
};
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 __using = (stack, value, async) => {
  if (value != null) {
    if (typeof value !== "object" && typeof value !== "function") __typeError("Object expected");
    var dispose, inner;
    if (async) dispose = value[__knownSymbol("asyncDispose")];
    if (dispose === void 0) {
      dispose = value[__knownSymbol("dispose")];
      if (async) inner = dispose;
    }
    if (typeof dispose !== "function") __typeError("Object not disposable");
    if (inner) dispose = function() {
      try {
        inner.call(this);
      } catch (e) {
        return Promise.reject(e);
      }
    };
    stack.push([async, dispose, value]);
  } else if (async) {
    stack.push([async]);
  }
  return value;
};
var __callDispose = (stack, error, hasError) => {
  var E = typeof SuppressedError === "function" ? SuppressedError : function(e, s, m, _) {
    return _ = Error(m), _.name = "SuppressedError", _.error = e, _.suppressed = s, _;
  };
  var fail = (e) => error = hasError ? new E(e, error, "An error was suppressed during disposal") : (hasError = true, e);
  var next = (it) => {
    while (it = stack.pop()) {
      try {
        var result = it[1] && it[1].call(it[2]);
        if (it[0]) return Promise.resolve(result).then(next, (e) => (fail(e), next()));
      } catch (e) {
        fail(e);
      }
    }
    if (hasError) throw error;
  };
  return next();
};
var definition_list_exports = {};
__export(definition_list_exports, {
  DefinitionList: () => DefinitionList
});
module.exports = __toCommonJS(definition_list_exports);
var import_markdown = require("./markdown.js");
var import_printer = require("./printer.js");
var import_style = require("./style.js");
var import_visible_width_of = require("./visible-width-of.js");
/**
 * @license
 * Copyright 2022-2026 Greg Lauckhart <greg@lauckhart.com>
 * SPDX-License-Identifier: Apache-2.0
 */
const DIM = (0, import_style.Style)([2]);
function DefinitionList(entries) {
  return {
    renderTo(printer) {
      if (!entries.length) {
        return;
      }
      const terminalWidth = printer.state.terminalWidth ?? 80;
      const maxNameWidth = Math.min(
        Math.max(...entries.map((e) => widthOf(e.name))),
        Math.floor(terminalWidth * 0.4)
      );
      const gutter = 2;
      const indent = 2;
      const nameCol = maxNameWidth + indent + gutter;
      for (const entry of entries) {
        var _stack2 = [];
        try {
          const nameWidth = widthOf(entry.name);
          {
            var _stack = [];
            try {
              const _nowrap = __using(_stack, printer.state({ wrap: false }));
              printer.write("".padEnd(indent));
              if (import_printer.Printer.isRenderable(entry.name)) {
                entry.name.renderTo(printer);
              } else {
                printer.write(entry.name);
              }
              if (nameWidth > maxNameWidth) {
                printer.write("\n", "".padEnd(nameCol));
              } else {
                printer.write("".padEnd(maxNameWidth - nameWidth + gutter));
              }
            } catch (_) {
              var _error = _, _hasError = true;
            } finally {
              __callDispose(_stack, _error, _hasError);
            }
          }
          const _cx = __using(_stack2, printer.state({ style: DIM, wrapIndent: nameCol }));
          if (import_printer.Printer.isRenderable(entry.description)) {
            entry.description.renderTo(printer);
          } else if (entry.description) {
            (0, import_markdown.Markdown)(entry.description).renderTo(printer);
          }
          printer.write("\n");
        } catch (_2) {
          var _error2 = _2, _hasError2 = true;
        } finally {
          __callDispose(_stack2, _error2, _hasError2);
        }
      }
    }
  };
}
function widthOf(name) {
  if (typeof name === "string") {
    return (0, import_visible_width_of.visibleWidthOf)(name);
  }
  return 0;
}
//# sourceMappingURL=definition-list.js.map
