"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const Vue = require("vue");
const index = require("../../../@vexip-ui/utils/dist/index.js");
const common = require("../helpers/common.js");
const draggable = require("../helpers/draggable.js");
const responsive = require("../helpers/responsive.js");
const dom = require("../helpers/dom.js");
const interact_min = require("../../../../_virtual/interact.min.js");
const st = /* @__PURE__ */ Vue.defineComponent({
  __name: "grid-item",
  props: {
    isDraggable: { type: Boolean, default: void 0 },
    isResizable: { type: Boolean, default: void 0 },
    isBounded: { type: Boolean, default: void 0 },
    static: { type: Boolean, default: false },
    minH: { default: 1 },
    minW: { default: 1 },
    maxH: { default: 1 / 0 },
    maxW: { default: 1 / 0 },
    x: {},
    y: {},
    w: {},
    h: {},
    i: {},
    dragIgnoreFrom: { default: "a, button" },
    dragAllowFrom: { default: void 0 },
    resizeIgnoreFrom: { default: "a, button" },
    preserveAspectRatio: { type: Boolean, default: false },
    dragOption: { default: () => ({}) },
    resizeOption: { default: () => ({}) }
  },
  emits: ["container-resized", "resize", "resized", "move", "moved"],
  setup(Se, { expose: Me, emit: We }) {
    const i = Se, M = We, l = Vue.inject(common.LAYOUT_KEY), g = Vue.inject(common.EMITTER_KEY);
    if (!l)
      throw new Error("[grid-layout-plus]: missing layout store, GridItem must under a GridLayout.");
    const d = Vue.shallowRef(null), e = Vue.reactive({
      cols: 1,
      containerWidth: 100,
      rowHeight: 30,
      margin: [10, 10],
      maxRows: 1 / 0,
      draggable: void 0,
      resizable: void 0,
      bounded: void 0,
      transformScale: 1,
      useCssTransforms: true,
      useStyleCursor: true,
      isDragging: false,
      dragging: {
        top: -1,
        left: -1
      },
      isResizing: false,
      resizing: {
        width: -1,
        height: -1
      },
      style: {},
      rtl: false
    });
    let X = false, _ = false, $ = NaN, G = NaN, k = NaN, E = NaN, j = -1, K = -1, V = -1, q = -1, h = i.x, v = i.y, p = i.w, y = i.h;
    const W = Vue.ref(), J = Vue.reactive({
      i: Vue.toRef(i, "i"),
      state: e,
      wrapper: W,
      calcXY: A
    });
    function Q(t) {
      Te(t);
    }
    function Z() {
      de();
    }
    function U(t) {
      index.isNull(i.isDraggable) && (e.draggable = t);
    }
    function ee(t) {
      index.isNull(i.isResizable) && (e.resizable = t);
    }
    function te(t) {
      index.isNull(i.isBounded) && (e.bounded = t);
    }
    function ie(t) {
      e.transformScale = t;
    }
    function ae(t) {
      e.rowHeight = t;
    }
    function re(t) {
      e.maxRows = t;
    }
    function ne() {
      e.rtl = dom.getDocumentDir() === "rtl", de();
    }
    function oe(t) {
      e.cols = Math.floor(t);
    }
    l.increaseItem(J), Vue.onBeforeMount(() => {
      e.rtl = dom.getDocumentDir() === "rtl";
    }), Vue.onMounted(() => {
      l.responsive && l.lastBreakpoint ? e.cols = responsive.getColsFromBreakpoint(l.lastBreakpoint, l.cols) : e.cols = l.colNum, e.rowHeight = l.rowHeight, e.containerWidth = l.width !== null ? l.width : 100, e.margin = l.margin !== void 0 ? l.margin.map(Number) : [10, 10], e.maxRows = l.maxRows, index.isNull(i.isDraggable) ? e.draggable = l.isDraggable : e.draggable = i.isDraggable, index.isNull(i.isResizable) ? e.resizable = l.isResizable : e.resizable = i.isResizable, index.isNull(i.isBounded) ? e.bounded = l.isBounded : e.bounded = i.isBounded, e.transformScale = l.transformScale, e.useCssTransforms = l.useCssTransforms, e.useStyleCursor = l.useStyleCursor, Vue.watchEffect(() => {
        h = i.x, v = i.y, y = i.h, p = i.w, index.nextTickOnce(H);
      }), g.on("updateWidth", Q), g.on("compact", Z), g.on("setDraggable", U), g.on("setResizable", ee), g.on("setBounded", te), g.on("setTransformScale", ie), g.on("setRowHeight", ae), g.on("setMaxRows", re), g.on("directionchange", ne), g.on("setColNum", oe);
    }), Vue.onBeforeUnmount(() => {
      g.off("updateWidth", Q), g.off("compact", Z), g.off("setDraggable", U), g.off("setResizable", ee), g.off("setBounded", te), g.off("setTransformScale", ie), g.off("setRowHeight", ae), g.off("setMaxRows", re), g.off("directionchange", ne), g.off("setColNum", oe), d.value && (d.value.unset(), d.value = null), l.decreaseItem(J);
    }), Me({ state: e, wrapper: W });
    const Be = typeof navigator < "u" ? navigator.userAgent.toLowerCase().includes("android") : false, se = Vue.computed(() => e.resizable && !i.static), m = Vue.computed(() => l.isMirrored ? !e.rtl : e.rtl), Ce = Vue.computed(() => (e.draggable || e.resizable) && !i.static), z = common.useNameHelper("item"), De = Vue.computed(() => ({
      [z.b()]: true,
      [z.bm("resizable")]: se.value,
      [z.bm("static")]: i.static,
      [z.bm("resizing")]: e.isResizing,
      [z.bm("dragging")]: e.isDragging,
      [z.bm("transform")]: e.useCssTransforms,
      [z.bm("rtl")]: m.value,
      [z.bm("no-touch")]: Be && Ce.value
    })), T = Vue.computed(() => [z.be("resizer"), m.value && z.bem("resizer", "rtl")].filter(Boolean));
    Vue.watch(
      () => i.isDraggable,
      (t) => {
        e.draggable = t;
      }
    ), Vue.watch(
      () => i.static,
      () => {
        index.nextTickOnce(ue), index.nextTickOnce(S);
      }
    ), Vue.watch(
      () => e.draggable,
      () => {
        index.nextTickOnce(ue);
      }
    ), Vue.watch(
      () => i.isResizable,
      (t) => {
        e.resizable = t;
      }
    ), Vue.watch(
      () => i.isBounded,
      (t) => {
        e.bounded = t;
      }
    ), Vue.watch(
      () => e.resizable,
      () => {
        index.nextTickOnce(S);
      }
    ), Vue.watch(
      () => e.rowHeight,
      () => {
        index.nextTickOnce(H), index.nextTickOnce(F);
      }
    ), Vue.watch([() => e.cols, () => e.containerWidth], () => {
      index.nextTickOnce(S), index.nextTickOnce(H), index.nextTickOnce(F);
    }), Vue.watch([() => i.minH, () => i.maxH, () => i.minW, () => i.maxW], () => {
      index.nextTickOnce(S);
    }), Vue.watch(m, () => {
      index.nextTickOnce(S), index.nextTickOnce(H);
    }), Vue.watch([() => l.margin, () => l.margin[0], () => l.margin[1]], () => {
      const t = l.margin;
      !t || t[0] === e.margin[0] && t[1] === e.margin[1] || (e.margin = t.map(Number), index.nextTickOnce(H), index.nextTickOnce(F));
    });
    function H() {
      i.x + i.w > e.cols ? (h = 0, p = i.w > e.cols ? e.cols : i.w) : (h = i.x, p = i.w);
      const t = B(h, v, p, y);
      e.isDragging && (t.top = e.dragging.top, m.value ? t.right = e.dragging.left : t.left = e.dragging.left), e.isResizing && (t.width = e.resizing.width, t.height = e.resizing.height);
      let r;
      e.useCssTransforms ? m.value ? r = common.setTransformRtl(t.top, t.right, t.width, t.height) : r = common.setTransform(t.top, t.left, t.width, t.height) : m.value ? r = common.setTopRight(t.top, t.right, t.width, t.height) : r = common.setTopLeft(t.top, t.left, t.width, t.height), e.style = r;
    }
    function F() {
      const t = {};
      for (const r of ["width", "height"]) {
        const o = e.style[r].match(/^(\d+)px$/);
        if (!o)
          return;
        t[r] = o[1];
      }
      M("container-resized", i.i, i.h, i.w, t.height, t.width);
    }
    function le(t) {
      if (i.static) return;
      const r = t.type;
      if (r === "resizestart" && e.isResizing || r !== "resizestart" && !e.isResizing)
        return;
      const s = draggable.getControlPosition(t);
      if (index.isNull(s)) return;
      const { x: o, y: c } = s, n = { width: 0, height: 0 };
      let a;
      switch (r) {
        case "resizestart": {
          S(), j = p, K = y, a = B(h, v, p, y), n.width = a.width, n.height = a.height, e.resizing = n, e.isResizing = true;
          break;
        }
        case "resizemove": {
          !t.edges.right && !t.edges.left && (k = o), !t.edges.top && !t.edges.bottom && (E = c);
          const u = draggable.createCoreData(k, E, o, c);
          m.value ? n.width = e.resizing.width - u.deltaX / e.transformScale : n.width = e.resizing.width + u.deltaX / e.transformScale, n.height = e.resizing.height + u.deltaY / e.transformScale, e.resizing = n;
          break;
        }
        case "resizeend": {
          a = B(h, v, p, y), n.width = a.width, n.height = a.height, e.resizing = { width: -1, height: -1 }, e.isResizing = false;
          break;
        }
      }
      a = Ne(n.height, n.width), a.w < i.minW && (a.w = i.minW), a.w > i.maxW && (a.w = i.maxW), a.h < i.minH && (a.h = i.minH), a.h > i.maxH && (a.h = i.maxH), a.h < 1 && (a.h = 1), a.w < 1 && (a.w = 1), k = o, E = c, (p !== a.w || y !== a.h) && M("resize", i.i, a.h, a.w, n.height, n.width), t.type === "resizeend" && (j !== p || K !== y) && M("resized", i.i, a.h, a.w, n.height, n.width), g.emit("resizeEvent", t.type, i.i, h, v, a.h, a.w);
    }
    function ge(t) {
      if (i.static || e.isResizing) return;
      const r = t.type;
      if (r === "dragstart" && e.isDragging || r !== "dragstart" && !e.isDragging)
        return;
      const s = draggable.getControlPosition(t);
      if (index.isNull(s)) return;
      const { x: o, y: c } = s, n = t.target;
      if (!n.offsetParent) return;
      const a = { top: 0, left: 0 };
      switch (r) {
        case "dragstart": {
          V = h, q = v;
          const w = n.offsetParent.getBoundingClientRect(), R = n.getBoundingClientRect(), C = R.left / e.transformScale, D = w.left / e.transformScale, P = R.right / e.transformScale, Y = w.right / e.transformScale, L = R.top / e.transformScale, O = w.top / e.transformScale;
          m.value ? a.left = (P - Y) * -1 : a.left = C - D, a.top = L - O, e.dragging = a, e.isDragging = true;
          break;
        }
        case "dragmove": {
          const w = draggable.createCoreData($, G, o, c);
          if (m.value ? a.left = e.dragging.left - w.deltaX / e.transformScale : a.left = e.dragging.left + w.deltaX / e.transformScale, a.top = e.dragging.top + w.deltaY / e.transformScale, e.bounded) {
            const R = n.offsetParent.clientHeight - ce(i.h, e.rowHeight, e.margin[1]);
            a.top = fe(a.top, 0, R);
            const C = I(), D = e.containerWidth - ce(i.w, C, e.margin[0]);
            a.left = fe(a.left, 0, D);
          }
          e.dragging = a;
          break;
        }
        case "dragend": {
          const w = n.offsetParent.getBoundingClientRect(), R = n.getBoundingClientRect(), C = R.left / e.transformScale, D = w.left / e.transformScale, P = R.right / e.transformScale, Y = w.right / e.transformScale, L = R.top / e.transformScale, O = w.top / e.transformScale;
          m.value ? a.left = (P - Y) * -1 : a.left = C - D, a.top = L - O, e.dragging = { top: -1, left: -1 }, e.isDragging = false;
          break;
        }
      }
      let u;
      m.value, u = A(a.top, a.left), $ = o, G = c, (h !== u.x || v !== u.y) && M("move", i.i, u.x, u.y), t.type === "dragend" && (V !== h || q !== v) && M("moved", i.i, u.x, u.y), g.emit("dragEvent", t.type, i.i, u.x, u.y, y, p);
    }
    function B(t, r, s, o) {
      const c = I();
      let n;
      return m.value ? n = {
        right: Math.round(c * t + (t + 1) * e.margin[0]),
        top: Math.round(e.rowHeight * r + (r + 1) * e.margin[1]),
        // 0 * Infinity === NaN, which causes problems with resize constraints;
        // Fix this if it occurs.
        // Note we do it here rather than later because Math.round(Infinity) causes depot
        width: s === 1 / 0 ? s : Math.round(c * s + Math.max(0, s - 1) * e.margin[0]),
        height: o === 1 / 0 ? o : Math.round(e.rowHeight * o + Math.max(0, o - 1) * e.margin[1])
      } : n = {
        left: Math.round(c * t + (t + 1) * e.margin[0]),
        top: Math.round(e.rowHeight * r + (r + 1) * e.margin[1]),
        // 0 * Infinity === NaN, which causes problems with resize constraints;
        // Fix this if it occurs.
        // Note we do it here rather than later because Math.round(Infinity) causes depot
        width: s === 1 / 0 ? s : Math.round(c * s + Math.max(0, s - 1) * e.margin[0]),
        height: o === 1 / 0 ? o : Math.round(e.rowHeight * o + Math.max(0, o - 1) * e.margin[1])
      }, n;
    }
    function A(t, r) {
      const s = I();
      let o = Math.round((r - e.margin[0]) / (s + e.margin[0])), c = Math.round((t - e.margin[1]) / (e.rowHeight + e.margin[1]));
      return o = Math.max(Math.min(o, e.cols - p), 0), c = Math.max(Math.min(c, e.maxRows - y), 0), { x: o, y: c };
    }
    function I() {
      return (e.containerWidth - e.margin[0] * (e.cols + 1)) / e.cols;
    }
    function ce(t, r, s) {
      return Number.isFinite(t) ? Math.round(r * t + Math.max(0, t - 1) * s) : t;
    }
    function fe(t, r, s) {
      return Math.max(Math.min(t, s), r);
    }
    function Ne(t, r, s = false) {
      const o = I();
      let c = Math.round((r + e.margin[0]) / (o + e.margin[0])), n = 0;
      return s ? n = Math.ceil((t + e.margin[1]) / (e.rowHeight + e.margin[1])) : n = Math.round((t + e.margin[1]) / (e.rowHeight + e.margin[1])), c = Math.max(Math.min(c, e.cols - h), 0), n = Math.max(Math.min(n, e.maxRows - v), 0), { w: c, h: n };
    }
    function Te(t, r) {
      e.containerWidth = t;
    }
    function de() {
      H();
    }
    function me() {
      !d.value && W.value && (d.value = interact_min.default(W.value), e.useStyleCursor || d.value.styleCursor(false));
    }
    const Ie = index.throttle(ge);
    function ue() {
      if (me(), !!d.value)
        if (e.draggable && !i.static) {
          const t = {
            ignoreFrom: i.dragIgnoreFrom,
            allowFrom: i.dragAllowFrom,
            ...i.dragOption
          };
          d.value.draggable(t), X || (X = true, d.value.on("dragstart dragmove dragend", (r) => {
            r.type === "dragmove" ? Ie(r) : ge(r);
          }));
        } else
          d.value.draggable({ enabled: false });
    }
    const ke = index.throttle(le);
    function S() {
      if (me(), !!d.value)
        if (e.resizable && !i.static) {
          const t = B(0, 0, i.maxW, i.maxH), r = B(0, 0, i.minW, i.minH), s = {
            edges: {
              left: m.value ? `.${T.value[0]}` : false,
              right: m.value ? false : `.${T.value[0]}`,
              bottom: `.${T.value[0]}`,
              top: false
            },
            ignoreFrom: i.resizeIgnoreFrom,
            restrictSize: {
              min: {
                height: r.height * e.transformScale,
                width: r.width * e.transformScale
              },
              max: {
                height: t.height * e.transformScale,
                width: t.width * e.transformScale
              }
            },
            ...i.resizeOption
          };
          i.preserveAspectRatio && (s.modifiers = [interact_min.default.modifiers.aspectRatio({ ratio: "preserve" })]), d.value.resizable(s), _ || (_ = true, d.value.on("resizestart resizemove resizeend", (o) => {
            o.type === "resizemove" ? ke(o) : le(o);
          }));
        } else
          d.value.resizable({ enabled: false });
    }
    return (t, r) => (Vue.openBlock(), Vue.createElementBlock("section", {
      ref_key: "wrapper",
      ref: W,
      class: Vue.normalizeClass(De.value),
      style: Vue.normalizeStyle(e.style)
    }, [
      Vue.renderSlot(t.$slots, "default"),
      se.value ? (Vue.openBlock(), Vue.createElementBlock("span", {
        key: 0,
        class: Vue.normalizeClass(T.value)
      }, null, 2)) : Vue.createCommentVNode("", true)
    ], 6));
  }
});
exports.default = st;
