"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const Vue = require("vue");
const index_esm = require("../../../@vant/use/dist/index.esm.js");
const useTouch = require("../composables/use-touch.js");
const useExpose = require("../composables/use-expose.js");
const props = require("../utils/props.js");
const create = require("../utils/create.js");
const basic = require("../utils/basic.js");
const interceptor = require("../utils/interceptor.js");
const dom = require("../utils/dom.js");
const format = require("../utils/format.js");
const [name, bem] = create.createNamespace("swipe-cell");
const swipeCellProps = {
  name: props.makeNumericProp(""),
  disabled: Boolean,
  leftWidth: props.numericProp,
  rightWidth: props.numericProp,
  threshold: {
    type: props.numericProp,
    default: 0.15,
    validator: (value) => +value >= 0 && +value <= 1
  },
  beforeClose: Function,
  stopPropagation: Boolean
};
var stdin_default = Vue.defineComponent({
  name,
  props: swipeCellProps,
  emits: ["open", "close", "click"],
  setup(props2, {
    emit,
    slots
  }) {
    let opened;
    let lockClick;
    let startOffset;
    let isInBeforeClosing;
    const root = Vue.ref();
    const leftRef = Vue.ref();
    const rightRef = Vue.ref();
    const state = Vue.reactive({
      offset: 0,
      dragging: false
    });
    const touch = useTouch.useTouch();
    const getWidthByRef = (ref2) => ref2.value ? index_esm.useRect(ref2).width : 0;
    const leftWidth = Vue.computed(() => basic.isDef(props2.leftWidth) ? +props2.leftWidth : getWidthByRef(leftRef));
    const rightWidth = Vue.computed(() => basic.isDef(props2.rightWidth) ? +props2.rightWidth : getWidthByRef(rightRef));
    const open = (side) => {
      state.offset = side === "left" ? leftWidth.value : -rightWidth.value;
      if (!opened) {
        opened = true;
        emit("open", {
          name: props2.name,
          position: side
        });
      }
    };
    const close = (position) => {
      state.offset = 0;
      if (opened) {
        opened = false;
        emit("close", {
          name: props2.name,
          position
        });
      }
    };
    const toggle = (side) => {
      const offset = Math.abs(state.offset);
      const thresholdValue = +props2.threshold;
      const threshold = opened ? 1 - thresholdValue : thresholdValue;
      const width = side === "left" ? leftWidth.value : rightWidth.value;
      if (width && offset > width * threshold) {
        open(side);
      } else {
        close(side);
      }
    };
    const onTouchStart = (event) => {
      if (!props2.disabled) {
        startOffset = state.offset;
        touch.start(event);
      }
    };
    const onTouchMove = (event) => {
      if (props2.disabled) {
        return;
      }
      const {
        deltaX
      } = touch;
      touch.move(event);
      if (touch.isHorizontal()) {
        lockClick = true;
        state.dragging = true;
        const isEdge = !opened || deltaX.value * startOffset < 0;
        if (isEdge) {
          dom.preventDefault(event, props2.stopPropagation);
        }
        state.offset = format.clamp(deltaX.value + startOffset, -rightWidth.value, leftWidth.value);
      }
    };
    const onTouchEnd = () => {
      if (state.dragging) {
        state.dragging = false;
        toggle(state.offset > 0 ? "left" : "right");
        setTimeout(() => {
          lockClick = false;
        }, 0);
      }
    };
    const onClick = (position = "outside", event) => {
      if (isInBeforeClosing) return;
      emit("click", position);
      if (opened && !lockClick) {
        isInBeforeClosing = true;
        interceptor.callInterceptor(props2.beforeClose, {
          args: [{
            event,
            name: props2.name,
            position
          }],
          done: () => {
            isInBeforeClosing = false;
            close(position);
          },
          canceled: () => isInBeforeClosing = false,
          error: () => isInBeforeClosing = false
        });
      }
    };
    const getClickHandler = (position) => (event) => {
      if (lockClick || opened) {
        event.stopPropagation();
      }
      if (lockClick) {
        return;
      }
      onClick(position, event);
    };
    const renderSideContent = (side, ref2) => {
      const contentSlot = slots[side];
      if (contentSlot) {
        return Vue.createVNode("div", {
          "ref": ref2,
          "class": bem(side),
          "onClick": getClickHandler(side)
        }, [contentSlot()]);
      }
    };
    useExpose.useExpose({
      open,
      close
    });
    index_esm.useClickAway(root, (event) => onClick("outside", event), {
      eventName: "touchstart"
    });
    index_esm.useEventListener("touchmove", onTouchMove, {
      target: root
    });
    return () => {
      var _a;
      const wrapperStyle = {
        transform: `translate3d(${state.offset}px, 0, 0)`,
        transitionDuration: state.dragging ? "0s" : ".6s"
      };
      return Vue.createVNode("div", {
        "ref": root,
        "class": bem(),
        "onClick": getClickHandler("cell"),
        "onTouchstartPassive": onTouchStart,
        "onTouchend": onTouchEnd,
        "onTouchcancel": onTouchEnd
      }, [Vue.createVNode("div", {
        "class": bem("wrapper"),
        "style": wrapperStyle
      }, [renderSideContent("left", leftRef), (_a = slots.default) == null ? void 0 : _a.call(slots), renderSideContent("right", rightRef)])]);
    };
  }
});
exports.default = stdin_default;
exports.swipeCellProps = swipeCellProps;
