import { defineComponent, toRefs, ref, computed, shallowRef, watchEffect, watch, createVNode, Fragment } from 'vue';
import '../../../utils/index.mjs';
import '../../../hooks/index.mjs';
import PortalWrapper from '../../portal/src/PortalWrapper.mjs';
import { triggerProps } from '../../vc-trigger/src/interface.mjs';
import Trigger from '../../vc-trigger/src/Trigger.mjs';
import useTarget from './hooks/useTarget.mjs';
import TourStep from './TourStep/index.mjs';
import Mask from './Mask.mjs';
import { getPlacements } from './placements.mjs';
import { arrayType, booleanType, functionType, someType, stringType, objectType } from '../../../utils/vue/propType.mjs';
import { initDefaultProps } from '../../../utils/vue/initDefaultProps.mjs';
import { useMergedState } from '../../../hooks/use-merged-state/index.mjs';
import { classNames } from '../../../utils/dom/class.mjs';

const CENTER_PLACEHOLDER = {
  left: "50%",
  top: "50%",
  width: "1px",
  height: "1px"
};
function tourProps() {
  const {
    builtinPlacements,
    popupAlign
  } = triggerProps();
  return {
    builtinPlacements,
    popupAlign,
    steps: arrayType(),
    open: booleanType(),
    defaultCurrent: {
      type: Number
    },
    current: {
      type: Number
    },
    onChange: functionType(),
    onClose: functionType(),
    onFinish: functionType(),
    mask: someType([Boolean, Object], true),
    arrow: someType([Boolean, Object], true),
    rootClassName: {
      type: String
    },
    placement: stringType("bottom"),
    prefixCls: {
      type: String,
      default: "rc-tour"
    },
    renderPanel: functionType(),
    gap: objectType(),
    animated: someType([Boolean, Object]),
    scrollIntoViewOptions: someType([Boolean, Object], true),
    zIndex: {
      type: Number,
      default: 1001
    }
  };
}
const Tour = /* @__PURE__ */ defineComponent({
  name: "Tour",
  inheritAttrs: false,
  props: initDefaultProps(tourProps(), {}),
  setup(props) {
    const {
      defaultCurrent,
      placement,
      mask,
      scrollIntoViewOptions,
      open,
      gap,
      arrow
    } = toRefs(props);
    const triggerRef = ref();
    const [mergedCurrent, setMergedCurrent] = useMergedState(0, {
      value: computed(() => props.current),
      defaultValue: defaultCurrent.value
    });
    const [mergedOpen, setMergedOpen] = useMergedState(void 0, {
      value: computed(() => props.open),
      postState: (origin) => mergedCurrent.value < 0 || mergedCurrent.value >= props.steps.length ? false : origin != null ? origin : true
    });
    const openRef = shallowRef(mergedOpen.value);
    watchEffect(() => {
      if (mergedOpen.value && !openRef.value)
        setMergedCurrent(0);
      openRef.value = mergedOpen.value;
    });
    const curStep = computed(() => props.steps[mergedCurrent.value] || {});
    const mergedPlacement = computed(() => {
      var _a;
      return (_a = curStep.value.placement) != null ? _a : placement.value;
    });
    const mergedMask = computed(() => {
      var _a;
      return mergedOpen.value && ((_a = curStep.value.mask) != null ? _a : mask.value);
    });
    const mergedScrollIntoViewOptions = computed(() => {
      var _a;
      return (_a = curStep.value.scrollIntoViewOptions) != null ? _a : scrollIntoViewOptions.value;
    });
    const [posInfo, targetElement] = useTarget(computed(() => curStep.value.target), open, gap, mergedScrollIntoViewOptions);
    const mergedArrow = computed(() => targetElement.value ? typeof curStep.value.arrow === "undefined" ? arrow.value : curStep.value.arrow : false);
    const arrowPointAtCenter = computed(() => typeof mergedArrow.value === "object" ? mergedArrow.value.pointAtCenter : false);
    watch(arrowPointAtCenter, () => {
      var _a;
      (_a = triggerRef.value) == null ? void 0 : _a.forcePopupAlign();
    });
    watch(mergedCurrent, () => {
      var _a;
      (_a = triggerRef.value) == null ? void 0 : _a.forcePopupAlign();
    });
    const onInternalChange = (nextCurrent) => {
      var _a;
      setMergedCurrent(nextCurrent);
      (_a = props.onChange) == null ? void 0 : _a.call(props, nextCurrent);
    };
    return () => {
      var _a;
      const {
        prefixCls,
        steps,
        onClose,
        onFinish,
        rootClassName,
        renderPanel,
        animated,
        zIndex,
        ...restProps
      } = props;
      if (targetElement.value === void 0)
        return null;
      const handleClose = () => {
        setMergedOpen(false);
        onClose == null ? void 0 : onClose(mergedCurrent.value);
      };
      const mergedShowMask = typeof mergedMask.value === "boolean" ? mergedMask.value : !!mergedMask.value;
      const mergedMaskStyle = typeof mergedMask.value === "boolean" ? void 0 : mergedMask.value;
      const getTriggerDOMNode = () => {
        return targetElement.value || document.body;
      };
      const getPopupElement = () => createVNode(TourStep, {
        "arrow": mergedArrow.value,
        "key": "content",
        "prefixCls": prefixCls,
        "total": steps.length,
        "renderPanel": renderPanel,
        "onPrev": () => {
          onInternalChange(mergedCurrent.value - 1);
        },
        "onNext": () => {
          onInternalChange(mergedCurrent.value + 1);
        },
        "onClose": handleClose,
        "current": mergedCurrent.value,
        "onFinish": () => {
          handleClose();
          onFinish == null ? void 0 : onFinish();
        },
        ...curStep.value
      }, null);
      const posInfoStyle = computed(() => {
        const info = posInfo.value || CENTER_PLACEHOLDER;
        const style = {};
        Object.keys(info).forEach((key) => {
          if (typeof info[key] === "number")
            style[key] = `${info[key]}px`;
          else
            style[key] = info[key];
        });
        return style;
      });
      return mergedOpen.value ? createVNode(Fragment, null, [createVNode(Mask, {
        "zIndex": zIndex,
        "prefixCls": prefixCls,
        "pos": posInfo.value,
        "showMask": mergedShowMask,
        "style": mergedMaskStyle == null ? void 0 : mergedMaskStyle.style,
        "fill": mergedMaskStyle == null ? void 0 : mergedMaskStyle.color,
        "open": mergedOpen.value,
        "animated": animated,
        "rootClassName": rootClassName
      }, null), createVNode(Trigger, {
        ...restProps,
        "builtinPlacements": !curStep.value.target ? void 0 : (_a = restProps.builtinPlacements) != null ? _a : getPlacements(arrowPointAtCenter.value),
        "ref": triggerRef,
        "popupStyle": !curStep.value.target ? {
          ...curStep.value.style,
          position: "fixed",
          left: CENTER_PLACEHOLDER.left,
          top: CENTER_PLACEHOLDER.top,
          transform: "translate(-50%, -50%)"
        } : curStep.value.style,
        "popupPlacement": mergedPlacement.value,
        "popupVisible": mergedOpen.value,
        "popupClassName": classNames(rootClassName, curStep.value.className),
        "prefixCls": prefixCls,
        "popup": getPopupElement,
        "forceRender": false,
        "destroyPopupOnHide": true,
        "zIndex": zIndex,
        "mask": false,
        "getTriggerDOMNode": getTriggerDOMNode
      }, {
        default: () => [createVNode(PortalWrapper, {
          "visible": mergedOpen.value,
          "autoLock": true
        }, {
          default: () => [createVNode("div", {
            "class": classNames(rootClassName, `${prefixCls}-target-placeholder`),
            "style": {
              ...posInfoStyle.value,
              position: "fixed",
              pointerEvents: "none"
            }
          }, null)]
        })]
      })]) : null;
    };
  }
});

export { Tour as default, tourProps };
//# sourceMappingURL=Tour.mjs.map
