'use strict';

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

var vue = require('vue');
require('../../../utils/index.cjs');
require('../../../hooks/index.cjs');
var PortalWrapper = require('../../portal/src/PortalWrapper.cjs');
var _interface = require('../../vc-trigger/src/interface.cjs');
var Trigger = require('../../vc-trigger/src/Trigger.cjs');
var useTarget = require('./hooks/useTarget.cjs');
var index$1 = require('./TourStep/index.cjs');
var Mask = require('./Mask.cjs');
var placements = require('./placements.cjs');
var propType = require('../../../utils/vue/propType.cjs');
var initDefaultProps = require('../../../utils/vue/initDefaultProps.cjs');
var index = require('../../../hooks/use-merged-state/index.cjs');
var _class = require('../../../utils/dom/class.cjs');

const CENTER_PLACEHOLDER = {
  left: "50%",
  top: "50%",
  width: "1px",
  height: "1px"
};
function tourProps() {
  const {
    builtinPlacements,
    popupAlign
  } = _interface.triggerProps();
  return {
    builtinPlacements,
    popupAlign,
    steps: propType.arrayType(),
    open: propType.booleanType(),
    defaultCurrent: {
      type: Number
    },
    current: {
      type: Number
    },
    onChange: propType.functionType(),
    onClose: propType.functionType(),
    onFinish: propType.functionType(),
    mask: propType.someType([Boolean, Object], true),
    arrow: propType.someType([Boolean, Object], true),
    rootClassName: {
      type: String
    },
    placement: propType.stringType("bottom"),
    prefixCls: {
      type: String,
      default: "rc-tour"
    },
    renderPanel: propType.functionType(),
    gap: propType.objectType(),
    animated: propType.someType([Boolean, Object]),
    scrollIntoViewOptions: propType.someType([Boolean, Object], true),
    zIndex: {
      type: Number,
      default: 1001
    }
  };
}
const Tour = /* @__PURE__ */ vue.defineComponent({
  name: "Tour",
  inheritAttrs: false,
  props: initDefaultProps.initDefaultProps(tourProps(), {}),
  setup(props) {
    const {
      defaultCurrent,
      placement,
      mask,
      scrollIntoViewOptions,
      open,
      gap,
      arrow
    } = vue.toRefs(props);
    const triggerRef = vue.ref();
    const [mergedCurrent, setMergedCurrent] = index.useMergedState(0, {
      value: vue.computed(() => props.current),
      defaultValue: defaultCurrent.value
    });
    const [mergedOpen, setMergedOpen] = index.useMergedState(void 0, {
      value: vue.computed(() => props.open),
      postState: (origin) => mergedCurrent.value < 0 || mergedCurrent.value >= props.steps.length ? false : origin != null ? origin : true
    });
    const openRef = vue.shallowRef(mergedOpen.value);
    vue.watchEffect(() => {
      if (mergedOpen.value && !openRef.value)
        setMergedCurrent(0);
      openRef.value = mergedOpen.value;
    });
    const curStep = vue.computed(() => props.steps[mergedCurrent.value] || {});
    const mergedPlacement = vue.computed(() => {
      var _a;
      return (_a = curStep.value.placement) != null ? _a : placement.value;
    });
    const mergedMask = vue.computed(() => {
      var _a;
      return mergedOpen.value && ((_a = curStep.value.mask) != null ? _a : mask.value);
    });
    const mergedScrollIntoViewOptions = vue.computed(() => {
      var _a;
      return (_a = curStep.value.scrollIntoViewOptions) != null ? _a : scrollIntoViewOptions.value;
    });
    const [posInfo, targetElement] = useTarget.default(vue.computed(() => curStep.value.target), open, gap, mergedScrollIntoViewOptions);
    const mergedArrow = vue.computed(() => targetElement.value ? typeof curStep.value.arrow === "undefined" ? arrow.value : curStep.value.arrow : false);
    const arrowPointAtCenter = vue.computed(() => typeof mergedArrow.value === "object" ? mergedArrow.value.pointAtCenter : false);
    vue.watch(arrowPointAtCenter, () => {
      var _a;
      (_a = triggerRef.value) == null ? void 0 : _a.forcePopupAlign();
    });
    vue.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 = () => vue.createVNode(index$1.default, {
        "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 = vue.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 ? vue.createVNode(vue.Fragment, null, [vue.createVNode(Mask.default, {
        "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), vue.createVNode(Trigger.default, {
        ...restProps,
        "builtinPlacements": !curStep.value.target ? void 0 : (_a = restProps.builtinPlacements) != null ? _a : placements.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": _class.classNames(rootClassName, curStep.value.className),
        "prefixCls": prefixCls,
        "popup": getPopupElement,
        "forceRender": false,
        "destroyPopupOnHide": true,
        "zIndex": zIndex,
        "mask": false,
        "getTriggerDOMNode": getTriggerDOMNode
      }, {
        default: () => [vue.createVNode(PortalWrapper.default, {
          "visible": mergedOpen.value,
          "autoLock": true
        }, {
          default: () => [vue.createVNode("div", {
            "class": _class.classNames(rootClassName, `${prefixCls}-target-placeholder`),
            "style": {
              ...posInfoStyle.value,
              position: "fixed",
              pointerEvents: "none"
            }
          }, null)]
        })]
      })]) : null;
    };
  }
});

exports.default = Tour;
exports.tourProps = tourProps;
//# sourceMappingURL=Tour.cjs.map
