/**
 * ldesign v0.4.2
 * (c) 2023 ldesign
 * @license MIT
 */

import { i as isNull_1 } from '../../../_chunks/dep-6f89b77e.mjs';
import { calculateNodeSize } from './helper.mjs';
import '../../../_chunks/dep-a0b4a044.mjs';
import '../../../_chunks/dep-e091cfd9.mjs';
import '../../../_chunks/dep-cfcedf47.mjs';
import '../../../_chunks/dep-b6a73f93.mjs';
import '../../../_chunks/dep-55846619.mjs';
import '../../../_chunks/dep-eb20e23f.mjs';
import '../../../_chunks/dep-b4041200.mjs';
import '../../../_chunks/dep-1cf2b129.mjs';
import '../../../_chunks/dep-c959a6fb.mjs';
import '../../../_chunks/dep-e3b14d70.mjs';
import '../../../_chunks/dep-66d6733b.mjs';
import '../../../_chunks/dep-c31e1d9e.mjs';
import '../../../_chunks/dep-30451c48.mjs';
import '../../../_chunks/dep-6eea369f.mjs';

var TEXTAREA_STYLE = "\n  min-height:0 !important;\n  max-height:none !important;\n  height:0 !important;\n  visibility:hidden !important;\n  overflow:hidden !important;\n  position:absolute !important;\n  z-index:-1000 !important;\n  top:0 !important;\n  right:0 !important\n";
var hiddenTextarea;
function calcTextareaHeight(targetElement) {
  var _hiddenTextarea, _hiddenTextarea$paren;
  var minRows = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
  var maxRows = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
  if (!hiddenTextarea) {
    hiddenTextarea = document.createElement("textarea");
    document.body.appendChild(hiddenTextarea);
  }
  var _calculateNodeSize = calculateNodeSize(targetElement),
    paddingSize = _calculateNodeSize.paddingSize,
    borderSize = _calculateNodeSize.borderSize,
    boxSizing = _calculateNodeSize.boxSizing,
    sizingStyle = _calculateNodeSize.sizingStyle;
  hiddenTextarea.setAttribute("style", "".concat(sizingStyle, ";").concat(TEXTAREA_STYLE));
  hiddenTextarea.value = targetElement.value || targetElement.placeholder || "";
  var height = hiddenTextarea.scrollHeight;
  var result = {};
  var isBorderbox = boxSizing === "border-box";
  var isContentbox = boxSizing === "content-box";
  if (isBorderbox) height += borderSize;else if (isContentbox) height -= paddingSize;
  hiddenTextarea.value = "";
  var singleRowHeight = hiddenTextarea.scrollHeight - paddingSize;
  (_hiddenTextarea = hiddenTextarea) === null || _hiddenTextarea === void 0 ? void 0 : (_hiddenTextarea$paren = _hiddenTextarea.parentNode) === null || _hiddenTextarea$paren === void 0 ? void 0 : _hiddenTextarea$paren.removeChild(hiddenTextarea);
  hiddenTextarea = null;
  var calcHeight = function calcHeight(rows) {
    var rowsHeight = singleRowHeight * rows;
    if (isBorderbox) rowsHeight = rowsHeight + paddingSize + borderSize;
    return rowsHeight;
  };
  if (!isNull_1(minRows)) {
    var minHeight = calcHeight(minRows);
    height = Math.max(minHeight, height);
    result.minHeight = "".concat(minHeight, "px");
  }
  if (!isNull_1(maxRows)) height = Math.min(calcHeight(maxRows), height);
  result.height = "".concat(height, "px");
  return result;
}

export { calcTextareaHeight as default };
//# sourceMappingURL=calcTextareaHeight.mjs.map
