'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var vue = require('vue');
require('../../../utils/index.cjs');
var props = require('./props.cjs');
var _class = require('../../../utils/dom/class.cjs');

function calcThumbStyle(targetElement) {
  return targetElement ? {
    left: targetElement.offsetLeft,
    right: targetElement.parentElement.clientWidth - targetElement.clientWidth - targetElement.offsetLeft,
    width: targetElement.clientWidth
  } : null;
}
const toPX = (value) => value !== void 0 ? `${value}px` : void 0;
var MotionThumb = /* @__PURE__ */ vue.defineComponent({
  props: props.motionThumbProps(),
  name: "AMotionThumb",
  emits: ["motionStart", "motionEnd"],
  setup(props, {
    emit
  }) {
    const thumbRef = vue.ref();
    const findValueElement = (val) => {
      var _a;
      const index = props.getValueIndex(val);
      const ele = (_a = props.containerRef.value) == null ? void 0 : _a.querySelectorAll(`.${props.prefixCls}-item`)[index];
      return (ele == null ? void 0 : ele.offsetParent) && ele;
    };
    const prevStyle = vue.ref(null);
    const nextStyle = vue.ref(null);
    vue.watch(() => props.value, (value, prevValue) => {
      const prev = findValueElement(prevValue);
      const next = findValueElement(value);
      const calcPrevStyle = calcThumbStyle(prev);
      const calcNextStyle = calcThumbStyle(next);
      prevStyle.value = calcPrevStyle;
      nextStyle.value = calcNextStyle;
      if (prev && next)
        emit("motionStart");
      else
        emit("motionEnd");
    }, {
      flush: "post"
    });
    const thumbStart = vue.computed(() => {
      var _a, _b;
      return props.direction === "rtl" ? toPX(-((_a = prevStyle.value) == null ? void 0 : _a.right)) : toPX((_b = prevStyle.value) == null ? void 0 : _b.left);
    });
    const thumbActive = vue.computed(() => {
      var _a, _b;
      return props.direction === "rtl" ? toPX(-((_a = nextStyle.value) == null ? void 0 : _a.right)) : toPX((_b = nextStyle.value) == null ? void 0 : _b.left);
    });
    let timeid;
    const onAppearStart = (el) => {
      clearTimeout(timeid);
      vue.nextTick(() => {
        if (el) {
          el.style.transform = "translateX(var(--thumb-start-left))";
          el.style.width = "var(--thumb-start-width)";
        }
      });
    };
    const onAppearActive = (el) => {
      timeid = setTimeout(() => {
        if (el) {
          _class.addClass(el, `${props.motionName}-appear-active`);
          el.style.transform = "translateX(var(--thumb-active-left))";
          el.style.width = "var(--thumb-active-width)";
        }
      });
    };
    const onAppearEnd = (el) => {
      prevStyle.value = null;
      nextStyle.value = null;
      if (el) {
        el.style.transform = null;
        el.style.width = null;
        _class.removeClass(el, `${props.motionName}-appear-active`);
      }
      emit("motionEnd");
    };
    const mergedStyle = vue.computed(() => {
      var _a, _b;
      return {
        "--thumb-start-left": thumbStart.value,
        "--thumb-start-width": toPX((_a = prevStyle.value) == null ? void 0 : _a.width),
        "--thumb-active-left": thumbActive.value,
        "--thumb-active-width": toPX((_b = nextStyle.value) == null ? void 0 : _b.width)
      };
    });
    vue.onBeforeUnmount(() => {
      clearTimeout(timeid);
    });
    return () => {
      const motionProps = {
        ref: thumbRef,
        style: mergedStyle.value,
        class: [`${props.prefixCls}-thumb`]
      };
      if (process.env.NODE_ENV === "test")
        motionProps["data-test-style"] = JSON.stringify(mergedStyle.value);
      return vue.createVNode(vue.Transition, {
        "appear": true,
        "onBeforeEnter": onAppearStart,
        "onEnter": onAppearActive,
        "onAfterEnter": onAppearEnd
      }, {
        default: () => [!prevStyle.value || !nextStyle.value ? null : vue.createVNode("div", motionProps, null)]
      });
    };
  }
});

exports.default = MotionThumb;
//# sourceMappingURL=MotionThumb.cjs.map
