"use strict";
const vue = require("vue");
const useDefaults = require("./useDefaults-YXpmugKp.js");
const useFormInput = require("./useFormInput-CSIV1X5N.js");
const dom = require("./dom-BgEUZpKg.js");
const index = require("./index-JJEBhaXq.js");
const useTextareaResize = (input, {
  maxRows,
  noAutoShrink,
  rows
}) => {
  const height = vue.ref(0);
  const maxRowsNumber = index.useToNumber(
    vue.computed(() => vue.toValue(maxRows) || Number.NaN),
    {
      method: "parseInt",
      nanToZero: true
    }
  );
  const rowsNumber = index.useToNumber(rows, {
    method: "parseInt",
    nanToZero: true
  });
  const computedMinRows = vue.computed(() => Math.max(rowsNumber.value || 2, 2));
  const computedMaxRows = vue.computed(() => Math.max(computedMinRows.value, maxRowsNumber.value || 0));
  const computedRows = vue.computed(
    () => computedMinRows.value === computedMaxRows.value ? computedMinRows.value : null
  );
  const handleHeightChange = async () => {
    if (!input.value || !dom.isVisible(input.value)) {
      height.value = null;
      return;
    }
    const computedStyle = getComputedStyle(input.value);
    const lineHeight = Number.parseFloat(computedStyle.lineHeight) || 1;
    const border = (Number.parseFloat(computedStyle.borderTopWidth) || 0) + (Number.parseFloat(computedStyle.borderBottomWidth) || 0);
    const padding = (Number.parseFloat(computedStyle.paddingTop) || 0) + (Number.parseFloat(computedStyle.paddingBottom) || 0);
    const offset = border + padding;
    const minHeight = lineHeight * computedMinRows.value + offset;
    const oldHeight = input.value.style.height || computedStyle.height;
    height.value = "auto";
    await vue.nextTick();
    if (!input.value) return;
    const { scrollHeight } = input.value;
    height.value = oldHeight;
    await vue.nextTick();
    if (!input.value) return;
    const contentRows = Math.max((scrollHeight - padding) / lineHeight, 2);
    const rows2 = Math.min(Math.max(contentRows, computedMinRows.value), computedMaxRows.value);
    const newHeight = Math.max(Math.ceil(rows2 * lineHeight + offset), minHeight);
    if (vue.toValue(noAutoShrink) && (Number.parseFloat(oldHeight.toString()) || 0) > newHeight) {
      height.value = oldHeight;
      return;
    }
    height.value = `${newHeight}px`;
  };
  vue.onMounted(handleHeightChange);
  const computedStyles = vue.computed(() => ({
    resize: "none",
    height: typeof height.value === "string" ? height.value : height.value ? `${height.value}px` : void 0
  }));
  return {
    onInput: handleHeightChange,
    computedStyles,
    computedRows
  };
};
const _hoisted_1 = ["id", "name", "form", "value", "disabled", "placeholder", "required", "autocomplete", "readonly", "aria-required", "aria-invalid", "rows", "wrap"];
const _sfc_main = /* @__PURE__ */ vue.defineComponent({
  __name: "BFormTextarea",
  props: /* @__PURE__ */ vue.mergeModels({
    noResize: { type: Boolean, default: false },
    rows: { default: 2 },
    wrap: { default: "soft" },
    noAutoShrink: { type: Boolean, default: false },
    maxRows: { default: void 0 },
    ariaInvalid: { type: [Boolean, String], default: void 0 },
    autocomplete: { default: void 0 },
    autofocus: { type: Boolean, default: false },
    disabled: { type: Boolean, default: false },
    form: { default: void 0 },
    formatter: { type: Function, default: void 0 },
    id: { default: void 0 },
    lazyFormatter: { type: Boolean, default: false },
    list: { default: void 0 },
    name: { default: void 0 },
    placeholder: { default: void 0 },
    plaintext: { type: Boolean, default: false },
    readonly: { type: Boolean, default: false },
    required: { type: Boolean, default: false },
    size: { default: void 0 },
    state: { type: [Boolean, null], default: void 0 },
    debounce: { default: 0 },
    debounceMaxWait: { default: Number.NaN }
  }, {
    "modelValue": {
      default: ""
    },
    "modelModifiers": {}
  }),
  emits: ["update:modelValue"],
  setup(__props, { expose: __expose }) {
    const _props = __props;
    const props = useDefaults.useDefaults(_props, "BFormTextarea");
    const [modelValue, modelModifiers] = vue.useModel(__props, "modelValue", {
      set: (v) => useFormInput.normalizeInput(v, modelModifiers)
    });
    const input = vue.useTemplateRef("_input");
    const {
      computedId,
      computedAriaInvalid,
      onInput,
      stateClass,
      onChange,
      onBlur,
      focus,
      blur,
      isDisabled
    } = useFormInput.useFormInput(props, input, modelValue, modelModifiers);
    const computedClasses = vue.computed(() => [
      stateClass.value,
      props.plaintext ? "form-control-plaintext" : "form-control",
      {
        [`form-control-${props.size}`]: !!props.size
      }
    ]);
    const {
      computedStyles: resizeStyles,
      onInput: handleHeightChange,
      computedRows
    } = useTextareaResize(input, {
      maxRows: () => props.maxRows,
      rows: () => props.rows,
      noAutoShrink: () => props.noAutoShrink
    });
    const computedStyles = vue.computed(() => ({
      resize: props.noResize ? "none" : void 0,
      ...props.maxRows || props.noAutoShrink ? resizeStyles.value : void 0
    }));
    __expose({
      blur,
      element: input,
      flushDebounce: onBlur,
      focus
    });
    return (_ctx, _cache) => {
      return vue.openBlock(), vue.createElementBlock("textarea", {
        id: vue.unref(computedId),
        ref: "_input",
        class: vue.normalizeClass(computedClasses.value),
        name: vue.unref(props).name || void 0,
        form: vue.unref(props).form || void 0,
        value: vue.unref(modelValue) ?? void 0,
        disabled: vue.unref(isDisabled),
        placeholder: vue.unref(props).placeholder,
        required: vue.unref(props).required || void 0,
        autocomplete: vue.unref(props).autocomplete || void 0,
        readonly: vue.unref(props).readonly || vue.unref(props).plaintext,
        "aria-required": vue.unref(props).required || void 0,
        "aria-invalid": vue.unref(computedAriaInvalid),
        rows: vue.unref(computedRows) || 2,
        style: vue.normalizeStyle(computedStyles.value),
        wrap: vue.unref(props).wrap || void 0,
        onInput: _cache[0] || (_cache[0] = (e) => {
          vue.unref(onInput)(e);
          vue.unref(handleHeightChange)();
        }),
        onChange: _cache[1] || (_cache[1] = //@ts-ignore
        (...args) => vue.unref(onChange) && vue.unref(onChange)(...args)),
        onBlur: _cache[2] || (_cache[2] = //@ts-ignore
        (...args) => vue.unref(onBlur) && vue.unref(onBlur)(...args))
      }, null, 46, _hoisted_1);
    };
  }
});
exports._sfc_main = _sfc_main;
//# sourceMappingURL=BFormTextarea.vue_vue_type_script_setup_true_lang-B99OqA4M.js.map
