"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const style = require("../../../../utils/dom/style.js");
const index = require("../../../../../node_modules/@vueuse/core/index.js");
const Vue = require("vue");
const index$1 = require("../../../../../node_modules/@vueuse/shared/index.js");
function useResizable(props, target, emit) {
  const { width, height } = index.useWindowSize();
  const isHorizontal = Vue.computed(() => ["ltr", "rtl"].includes(props.direction));
  const sign = Vue.computed(() => ["ltr", "ttb"].includes(props.direction) ? 1 : -1);
  const windowSize = Vue.computed(() => isHorizontal.value ? width.value : height.value);
  const getSize = Vue.computed(() => {
    return index$1.clamp(startSize.value + sign.value * offset.value, 4, windowSize.value);
  });
  const startSize = Vue.ref(0);
  const offset = Vue.ref(0);
  const isResizing = Vue.ref(false);
  const hasStartedDragging = Vue.ref(false);
  let startPos = [];
  let cleanups = [];
  const getActualSize = () => {
    var _a;
    const drawerEl = (_a = target.value) == null ? void 0 : _a.closest('[aria-modal="true"]');
    if (drawerEl) return isHorizontal.value ? drawerEl.offsetWidth : drawerEl.offsetHeight;
    return 100;
  };
  Vue.watch(() => [props.size, props.resizable], () => {
    hasStartedDragging.value = false;
    startSize.value = 0;
    offset.value = 0;
    onMouseUp();
  });
  const onMousedown = (e) => {
    if (!props.resizable) return;
    if (!hasStartedDragging.value) {
      startSize.value = getActualSize();
      hasStartedDragging.value = true;
    }
    startPos = [e.pageX, e.pageY];
    isResizing.value = true;
    emit("resize-start", e, startSize.value);
    cleanups.push(index.useEventListener(window, "mouseup", onMouseUp), index.useEventListener(window, "mousemove", onMouseMove));
  };
  const onMouseMove = (e) => {
    const { pageX, pageY } = e;
    const offsetX = pageX - startPos[0];
    const offsetY = pageY - startPos[1];
    offset.value = isHorizontal.value ? offsetX : offsetY;
    emit("resize", e, getSize.value);
  };
  const onMouseUp = (e) => {
    if (!isResizing.value) return;
    startPos = [];
    startSize.value = getSize.value;
    offset.value = 0;
    isResizing.value = false;
    cleanups.forEach((cleanup2) => cleanup2 == null ? void 0 : cleanup2());
    cleanups = [];
    if (e) emit("resize-end", e, startSize.value);
  };
  const cleanup = index.useEventListener(target, "mousedown", onMousedown);
  Vue.onBeforeUnmount(() => {
    cleanup();
    onMouseUp();
  });
  return {
    size: Vue.computed(() => {
      return hasStartedDragging.value ? `${getSize.value}px` : style.addUnit(props.size);
    }),
    isResizing,
    isHorizontal
  };
}
exports.useResizable = useResizable;
