"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 index = require("../loading/index.js");
const props = require("../utils/props.js");
const create = require("../utils/create.js");
const dom = require("../utils/dom.js");
const [name, bem, t] = create.createNamespace("pull-refresh");
const DEFAULT_HEAD_HEIGHT = 50;
const TEXT_STATUS = ["pulling", "loosing", "success"];
const pullRefreshProps = {
  disabled: Boolean,
  modelValue: Boolean,
  headHeight: props.makeNumericProp(DEFAULT_HEAD_HEIGHT),
  successText: String,
  pullingText: String,
  loosingText: String,
  loadingText: String,
  pullDistance: props.numericProp,
  successDuration: props.makeNumericProp(500),
  animationDuration: props.makeNumericProp(300)
};
var stdin_default = Vue.defineComponent({
  name,
  props: pullRefreshProps,
  emits: ["change", "refresh", "update:modelValue"],
  setup(props2, {
    emit,
    slots
  }) {
    let reachTop;
    const root = Vue.ref();
    const track = Vue.ref();
    const scrollParent = index_esm.useScrollParent(root);
    const state = Vue.reactive({
      status: "normal",
      distance: 0,
      duration: 0
    });
    const touch = useTouch.useTouch();
    const getHeadStyle = () => {
      if (props2.headHeight !== DEFAULT_HEAD_HEIGHT) {
        return {
          height: `${props2.headHeight}px`
        };
      }
    };
    const isTouchable = () => state.status !== "loading" && state.status !== "success" && !props2.disabled;
    const ease = (distance) => {
      const pullDistance = +(props2.pullDistance || props2.headHeight);
      if (distance > pullDistance) {
        if (distance < pullDistance * 2) {
          distance = pullDistance + (distance - pullDistance) / 2;
        } else {
          distance = pullDistance * 1.5 + (distance - pullDistance * 2) / 4;
        }
      }
      return Math.round(distance);
    };
    const setStatus = (distance, isLoading) => {
      const pullDistance = +(props2.pullDistance || props2.headHeight);
      state.distance = distance;
      if (isLoading) {
        state.status = "loading";
      } else if (distance === 0) {
        state.status = "normal";
      } else if (distance < pullDistance) {
        state.status = "pulling";
      } else {
        state.status = "loosing";
      }
      emit("change", {
        status: state.status,
        distance
      });
    };
    const getStatusText = () => {
      const {
        status
      } = state;
      if (status === "normal") {
        return "";
      }
      return props2[`${status}Text`] || t(status);
    };
    const renderStatus = () => {
      const {
        status,
        distance
      } = state;
      if (slots[status]) {
        return slots[status]({
          distance
        });
      }
      const nodes = [];
      if (TEXT_STATUS.includes(status)) {
        nodes.push(Vue.createVNode("div", {
          "class": bem("text")
        }, [getStatusText()]));
      }
      if (status === "loading") {
        nodes.push(Vue.createVNode(index.Loading, {
          "class": bem("loading")
        }, {
          default: getStatusText
        }));
      }
      return nodes;
    };
    const showSuccessTip = () => {
      state.status = "success";
      setTimeout(() => {
        setStatus(0);
      }, +props2.successDuration);
    };
    const checkPosition = (event) => {
      reachTop = dom.getScrollTop(scrollParent.value) === 0;
      if (reachTop) {
        state.duration = 0;
        touch.start(event);
      }
    };
    const onTouchStart = (event) => {
      if (isTouchable()) {
        checkPosition(event);
      }
    };
    const onTouchMove = (event) => {
      if (isTouchable()) {
        if (!reachTop) {
          checkPosition(event);
        }
        const {
          deltaY
        } = touch;
        touch.move(event);
        if (reachTop && deltaY.value >= 0 && touch.isVertical()) {
          dom.preventDefault(event);
          setStatus(ease(deltaY.value));
        }
      }
    };
    const onTouchEnd = () => {
      if (reachTop && touch.deltaY.value && isTouchable()) {
        state.duration = +props2.animationDuration;
        if (state.status === "loosing") {
          setStatus(+props2.headHeight, true);
          emit("update:modelValue", true);
          Vue.nextTick(() => emit("refresh"));
        } else {
          setStatus(0);
        }
      }
    };
    Vue.watch(() => props2.modelValue, (value) => {
      state.duration = +props2.animationDuration;
      if (value) {
        setStatus(+props2.headHeight, true);
      } else if (slots.success || props2.successText) {
        showSuccessTip();
      } else {
        setStatus(0, false);
      }
    });
    index_esm.useEventListener("touchmove", onTouchMove, {
      target: track
    });
    return () => {
      var _a;
      const trackStyle = {
        transitionDuration: `${state.duration}ms`,
        transform: state.distance ? `translate3d(0,${state.distance}px, 0)` : ""
      };
      return Vue.createVNode("div", {
        "ref": root,
        "class": bem()
      }, [Vue.createVNode("div", {
        "ref": track,
        "class": bem("track"),
        "style": trackStyle,
        "onTouchstartPassive": onTouchStart,
        "onTouchend": onTouchEnd,
        "onTouchcancel": onTouchEnd
      }, [Vue.createVNode("div", {
        "class": bem("head"),
        "style": getHeadStyle()
      }, [renderStatus()]), (_a = slots.default) == null ? void 0 : _a.call(slots)])]);
    };
  }
});
exports.default = stdin_default;
exports.pullRefreshProps = pullRefreshProps;
