import { defineComponent, ref, computed, watch, onMounted, onBeforeUnmount, resolveComponent, createElementBlock, openBlock, normalizeClass, unref, createCommentVNode, createElementVNode, normalizeStyle, renderSlot, withModifiers, createBlock, createVNode, withCtx } from 'vue';
import '../../../utils/index.mjs';
import { flexSplitLayoutProps } from './flex-split-layout.mjs';
import { createNamespace } from '../../../utils/create-namespace.mjs';

"use strict";
var _sfc_main = /* @__PURE__ */ defineComponent({
  ...{ name: "LlFlexSplitLayout" },
  __name: "flex-split-layout",
  props: flexSplitLayoutProps,
  emits: [
    "update:leftCurrentWidth",
    "update:foldState",
    "handleFold"
  ],
  setup(__props, { expose: __expose, emit: __emit }) {
    const bem = createNamespace("flex-split-layout");
    const props = __props;
    const emits = __emit;
    const leftWidth = ref(0);
    const isDragging = ref(false);
    const startX = ref(0);
    const startWidth = ref(0);
    const dragLayoutRef = ref();
    const leftPanelRef = ref();
    const layoutWidth = ref(0);
    const foldBtnRef = ref();
    const showDragBtn = computed(() => {
      return props.drag && props.isShowBtn && !(props.fold && foldState.value);
    });
    const preFoldWidth = ref(0);
    const foldState = computed({
      get: () => internalFoldState.value,
      set: (value) => {
        internalFoldState.value = value;
        emits("update:foldState", value);
        emits("handleFold", value);
      }
    });
    const internalFoldState = ref(props.foldState || false);
    const initLayoutWidth = () => {
      leftWidth.value = props.leftCurrentWidth > 0 ? props.leftCurrentWidth : props.leftDefaultWidth > 0 ? props.leftDefaultWidth : 0;
      preFoldWidth.value = leftWidth.value;
    };
    const constrainBoundaries = () => {
      if (!dragLayoutRef.value) return;
      const leftMin = props.leftMinWidth;
      const leftMax = Math.max(0, props.leftMaxWidth);
      const rightMin = Math.max(0, props.rightMinWidth);
      layoutWidth.value = dragLayoutRef.value.clientWidth;
      if (layoutWidth.value < leftMin + rightMin) {
        const safeWidth = Math.max(0, layoutWidth.value - rightMin);
        leftWidth.value = Math.min(leftMax, safeWidth);
        return;
      }
      if (leftWidth.value < leftMin) {
        leftWidth.value = leftMin;
      }
      if (leftWidth.value > leftMax) {
        leftWidth.value = leftMax;
      }
      if (layoutWidth.value - leftWidth.value < rightMin) {
        leftWidth.value = Math.max(0, layoutWidth.value - rightMin);
      }
      leftWidth.value = Math.max(0, leftWidth.value);
    };
    const handleFold = (state) => {
      if (!props.fold) return;
      const newState = state !== void 0 ? state : !foldState.value;
      if (newState) {
        preFoldWidth.value = leftWidth.value;
        leftWidth.value = 0;
      } else {
        const targetWidth = preFoldWidth.value > 0 ? preFoldWidth.value : props.leftDefaultWidth;
        leftWidth.value = targetWidth;
        constrainBoundaries();
      }
      foldState.value = newState;
    };
    const startDrag = (e) => {
      if (!props.drag || foldState.value && props.fold) return;
      isDragging.value = true;
      startX.value = e.clientX;
      startWidth.value = leftPanelRef.value.offsetWidth;
      layoutWidth.value = dragLayoutRef.value.clientWidth;
      constrainBoundaries();
      window.addEventListener("mousemove", onDrag);
      window.addEventListener("mouseup", stopDrag);
    };
    const stopDrag = () => {
      isDragging.value = false;
      emits("update:leftCurrentWidth", leftWidth.value);
      preFoldWidth.value = leftWidth.value;
      removeListeners();
    };
    const onDrag = (e) => {
      if (!isDragging.value || !leftPanelRef.value) return;
      const currentLayoutWidth = dragLayoutRef.value.clientWidth;
      const safeLeftMin = props.leftMinWidth;
      const safeLeftMax = Math.max(0, props.leftMaxWidth);
      const safeRightMin = Math.max(0, props.rightMinWidth);
      const maxAllowedLeftWidth = Math.max(0, currentLayoutWidth - safeRightMin);
      const deltaX = e.clientX - startX.value;
      let newWidth = startWidth.value + deltaX;
      newWidth = Math.min(
        Math.min(safeLeftMax, maxAllowedLeftWidth),
        Math.max(safeLeftMin, newWidth)
      );
      leftWidth.value = Math.max(0, newWidth);
    };
    const handleResize = () => {
      if (props.fold && foldState.value) return;
      constrainBoundaries();
    };
    watch(
      () => props.leftCurrentWidth,
      (val) => {
        if (!isDragging.value && !foldState.value) {
          leftWidth.value = val < 0 ? 0 : val;
          preFoldWidth.value = leftWidth.value;
        }
      }
    );
    watch(
      () => leftWidth.value,
      (val) => {
        if (!isDragging.value) {
          emits("update:leftCurrentWidth", val);
        }
      }
    );
    watch(
      () => props.foldState,
      (newState) => {
        if (newState !== foldState.value) {
          handleFold(newState);
        }
      }
    );
    const removeListeners = () => {
      window.removeEventListener("mousemove", onDrag);
      window.removeEventListener("mouseup", stopDrag);
    };
    onMounted(() => {
      window.addEventListener("resize", handleResize);
      initLayoutWidth();
      if (props.foldState) {
        handleFold(props.foldState);
        emits("update:leftCurrentWidth", leftWidth.value);
      }
    });
    onBeforeUnmount(() => {
      window.removeEventListener("resize", handleResize);
      removeListeners();
    });
    __expose({ handleFold });
    return (_ctx, _cache) => {
      const _component_ll_icon = resolveComponent("ll-icon");
      const _component_ll_tooltip = resolveComponent("ll-tooltip");
      return openBlock(), createElementBlock(
        "div",
        {
          ref_key: "dragLayoutRef",
          ref: dragLayoutRef,
          class: normalizeClass([unref(bem).b()])
        },
        [
          createCommentVNode(" \u5DE6\u4FA7 "),
          createElementVNode(
            "div",
            {
              ref_key: "leftPanelRef",
              ref: leftPanelRef,
              class: normalizeClass([
                unref(bem).e("left-panel"),
                showDragBtn.value ? unref(bem).is("drag-btn") : "",
                _ctx.speed ? unref(bem).is(_ctx.speed) : ""
              ]),
              style: normalizeStyle({
                width: props.flex && !foldState.value ? "auto" : `${leftWidth.value}px`
              })
            },
            [
              renderSlot(_ctx.$slots, "left-layout"),
              createElementVNode(
                "div",
                {
                  class: normalizeClass([
                    _ctx.drag || _ctx.fold ? unref(bem).e("divider") : "",
                    unref(bem).is(_ctx.drag && !(_ctx.fold && foldState.value) ? "drag" : "undrag")
                  ]),
                  style: normalizeStyle({
                    backgroundColor: leftWidth.value === 0 && !props.flex ? "transparent" : _ctx.dividerColor || "var(--color-split)"
                  }),
                  onMousedown: withModifiers(startDrag, ["prevent"])
                },
                [
                  showDragBtn.value ? (openBlock(), createBlock(_component_ll_icon, {
                    key: 0,
                    class: normalizeClass([unref(bem).e("divider-icon")]),
                    "icon-name": "icon-drag2",
                    size: 8
                  }, null, 8, ["class"])) : createCommentVNode("v-if", true),
                  createCommentVNode(" \u6298\u53E0\u6309\u94AE "),
                  _ctx.fold ? (openBlock(), createElementBlock(
                    "div",
                    {
                      key: 1,
                      ref_key: "foldBtnRef",
                      ref: foldBtnRef,
                      class: normalizeClass([unref(bem).e("fold-btn"), unref(bem).is(foldState.value ? "fold" : "unfold")]),
                      onMousedown: _cache[0] || (_cache[0] = withModifiers(() => {
                      }, ["stop"])),
                      onClick: _cache[1] || (_cache[1] = ($event) => handleFold(!foldState.value))
                    },
                    [
                      createVNode(_component_ll_tooltip, {
                        open: true,
                        tip: foldState.value ? "\u5C55\u5F00" : "\u6536\u8D77",
                        placement: "right",
                        arrow: false,
                        "get-popup-container": () => foldBtnRef.value
                      }, {
                        default: withCtx(() => [
                          createVNode(_component_ll_icon, {
                            "icon-name": foldState.value ? "icon-solid-arrow-right" : "icon-solid-arrow-left",
                            size: 12
                          }, null, 8, ["icon-name"])
                        ]),
                        _: 1
                        /* STABLE */
                      }, 8, ["tip", "get-popup-container"])
                    ],
                    34
                    /* CLASS, NEED_HYDRATION */
                  )) : createCommentVNode("v-if", true)
                ],
                38
                /* CLASS, STYLE, NEED_HYDRATION */
              )
            ],
            6
            /* CLASS, STYLE */
          ),
          createCommentVNode(" \u53F3\u4FA7 "),
          createElementVNode(
            "div",
            {
              class: normalizeClass(unref(bem).e("right-panel"))
            },
            [
              renderSlot(_ctx.$slots, "right-layout")
            ],
            2
            /* CLASS */
          )
        ],
        2
        /* CLASS */
      );
    };
  }
});

export { _sfc_main as default };
//# sourceMappingURL=flex-split-layout.vue2.mjs.map
