"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const error = require("../../../utils/error.js");
const index = require("../../../hooks/use-namespace/index.js");
const constants = require("./constants.js");
const index$1 = require("../../../../node_modules/@vueuse/core/index.js");
const Vue = require("vue");
const COMPONENT_NAME = "ElLabelWrap";
var form_label_wrap_default = /* @__PURE__ */ Vue.defineComponent({
  name: COMPONENT_NAME,
  props: {
    isAutoWidth: Boolean,
    updateAll: Boolean
  },
  setup(props, { slots }) {
    const formContext = Vue.inject(constants.formContextKey, void 0);
    const formItemContext = Vue.inject(constants.formItemContextKey);
    if (!formItemContext) error.throwError(COMPONENT_NAME, "usage: <el-form-item><label-wrap /></el-form-item>");
    const ns = index.useNamespace("form");
    const el = Vue.ref();
    const computedWidth = Vue.ref(0);
    const getLabelWidth = () => {
      var _a;
      if ((_a = el.value) == null ? void 0 : _a.firstElementChild) {
        const width = window.getComputedStyle(el.value.firstElementChild).width;
        return Math.ceil(Number.parseFloat(width));
      } else return 0;
    };
    const updateLabelWidth = (action = "update") => {
      Vue.nextTick(() => {
        if (slots.default && props.isAutoWidth) {
          if (action === "update") computedWidth.value = getLabelWidth();
          else if (action === "remove") formContext == null ? void 0 : formContext.deregisterLabelWidth(computedWidth.value);
        }
      });
    };
    const updateLabelWidthFn = () => updateLabelWidth("update");
    Vue.onMounted(() => {
      updateLabelWidthFn();
    });
    Vue.onBeforeUnmount(() => {
      updateLabelWidth("remove");
    });
    Vue.onUpdated(() => updateLabelWidthFn());
    Vue.watch(computedWidth, (val, oldVal) => {
      if (props.updateAll) formContext == null ? void 0 : formContext.registerLabelWidth(val, oldVal);
    });
    index$1.useResizeObserver(Vue.computed(() => {
      var _a;
      return ((_a = el.value) == null ? void 0 : _a.firstElementChild) ?? null;
    }), updateLabelWidthFn);
    return () => {
      var _a, _b;
      if (!slots) return null;
      const { isAutoWidth } = props;
      if (isAutoWidth) {
        const autoLabelWidth = formContext == null ? void 0 : formContext.autoLabelWidth;
        const hasLabel = formItemContext == null ? void 0 : formItemContext.hasLabel;
        const style = {};
        if (hasLabel && autoLabelWidth && autoLabelWidth !== "auto") {
          const marginWidth = Math.max(0, Number.parseInt(autoLabelWidth, 10) - computedWidth.value);
          const marginPosition = (formItemContext.labelPosition || formContext.labelPosition) === "left" ? "marginRight" : "marginLeft";
          if (marginWidth) style[marginPosition] = `${marginWidth}px`;
        }
        return Vue.createVNode("div", {
          "ref": el,
          "class": [ns.be("item", "label-wrap")],
          "style": style
        }, [(_a = slots.default) == null ? void 0 : _a.call(slots)]);
      } else return Vue.createVNode(Vue.Fragment, { "ref": el }, [(_b = slots.default) == null ? void 0 : _b.call(slots)]);
    };
  }
});
exports.default = form_label_wrap_default;
