/**
 * ldesign v0.3.13
 * (c) 2023 ldesign
 * @license MIT
 */

import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import { defineComponent, toRefs, ref, computed, nextTick, watch, onMounted, createVNode, h, Fragment, mergeProps, Teleport } from 'vue';
import isFunction from 'lodash/isFunction';
import setStyle from '../_common/js/utils/set-style.js';
import { isFixed, getWindowScroll, removeClass, addClass } from '../utils/dom.js';
import useVModel from '../hooks/useVModel.js';
import { useTNodeJSX } from '../hooks/tnode.js';
import { usePrefixClass } from '../hooks/useConfig.js';
import { Button } from '../button/index.js';
import { Popup } from '../popup/index.js';
import './utils/index.js';
import props from './props.js';
import { useConfig } from '../config-provider/useConfig.js';
import getRelativePosition from './utils/getRelativePosition.js';
import getTargetElm from './utils/getTargetElm.js';
import { scrollToParentVisibleArea } from './utils/getScrollParent.js';
import scrollToElm from './utils/scrollToElm.js';
import '../_chunks/dep-512eb181.js';
import 'lodash/isString';
import 'lodash/isArray';
import '../utils/easing.js';
import 'lodash/kebabCase';
import 'lodash/camelCase';
import '../utils/render-tnode.js';
import 'lodash/isEmpty';
import 'lodash/isObject';
import '../utils/withInstall.js';
import '../button/button.js';
import '@babel/runtime/helpers/toConsumableArray';
import '../loading/index.js';
import '../loading/directive.js';
import '../loading/plugin.js';
import '../loading/loading.js';
import '../hooks/useTeleport.js';
import '../loading/props.js';
import '../loading/icon/gradient.js';
import '../_common/js/loading/circle-adapter.js';
import '../_common/js/utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import 'lodash/isNull';
import 'lodash/isUndefined';
import 'lodash/isNumber';
import 'lodash/cloneDeep';
import '../config-provider/context.js';
import 'lodash/mergeWith';
import 'lodash/merge';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '@babel/runtime/helpers/typeof';
import 'dayjs';
import '../_chunks/dep-ac28b67c.js';
import '../config-provider/type.js';
import '../loading/style';
import '../loading/type.js';
import '../hooks/useRipple.js';
import '../utils/set-style.js';
import '../hooks/useKeepAnimation.js';
import '../form/hooks.js';
import '../button/props.js';
import '../button/style';
import '../button/type.js';
import '../popup/popup.js';
import '@popperjs/core';
import '../popup/props.js';
import '../popup/container.js';
import '../hooks/useResizeObserver.js';
import '../popup/style';
import '../popup/type.js';

var _Guide = defineComponent({
  name: "TGuide",
  props: props,
  setup: function setup(props2, context) {
    var renderTNodeJSX = useTNodeJSX();
    var COMPONENT_NAME = usePrefixClass("guide");
    var LOCK_CLASS = usePrefixClass("guide--lock");
    var _useConfig = useConfig("guide"),
      globalConfig = _useConfig.globalConfig;
    var _toRefs = toRefs(props2),
      current = _toRefs.current,
      modelValue = _toRefs.modelValue,
      hideCounter = _toRefs.hideCounter,
      hidePrev = _toRefs.hidePrev,
      hideSkip = _toRefs.hideSkip,
      steps = _toRefs.steps,
      zIndex = _toRefs.zIndex;
    var _useVModel = useVModel(current, modelValue, props2.defaultCurrent, props2.onChange, "current"),
      _useVModel2 = _slicedToArray(_useVModel, 2),
      innerCurrent = _useVModel2[0],
      setInnerCurrent = _useVModel2[1];
    var overlayLayerRef = ref();
    var highlightLayerRef = ref();
    var referenceLayerRef = ref();
    var currentHighlightLayerElm = ref();
    var dialogWrapperRef = ref();
    var dialogTooltipRef = ref();
    var actived = ref(false);
    var stepsTotal = computed(function () {
      return steps.value.length;
    });
    var currentStepInfo = computed(function () {
      return steps.value[innerCurrent.value];
    });
    var isPopup = computed(function () {
      return getCurrentCrossProps("mode") === "popup";
    });
    var currentElmIsFixed = computed(function () {
      return isFixed(currentHighlightLayerElm.value || document.body);
    });
    var getCurrentCrossProps = function getCurrentCrossProps(propsName) {
      var _currentStepInfo$valu;
      return (_currentStepInfo$valu = currentStepInfo.value[propsName]) !== null && _currentStepInfo$valu !== void 0 ? _currentStepInfo$valu : props2[propsName];
    };
    var setHighlightLayerPosition = function setHighlightLayerPosition(highlighLayer) {
      var _getRelativePosition = getRelativePosition(currentHighlightLayerElm.value),
        top = _getRelativePosition.top,
        left = _getRelativePosition.left;
      var _currentHighlightLaye = currentHighlightLayerElm.value.getBoundingClientRect(),
        width = _currentHighlightLaye.width,
        height = _currentHighlightLaye.height;
      var highlightPadding = getCurrentCrossProps("highlightPadding");
      if (isPopup.value) {
        width += highlightPadding * 2;
        height += highlightPadding * 2;
        top -= highlightPadding;
        left -= highlightPadding;
      } else {
        var _getWindowScroll = getWindowScroll(),
          scrollTop = _getWindowScroll.scrollTop,
          scrollLeft = _getWindowScroll.scrollLeft;
        top += scrollTop;
        left += scrollLeft;
      }
      setStyle(highlighLayer, {
        width: "".concat(width, "px"),
        height: "".concat(height, "px"),
        top: "".concat(top, "px"),
        left: "".concat(left, "px")
      });
    };
    var showPopupGuide = function showPopupGuide() {
      nextTick(function () {
        currentHighlightLayerElm.value = getTargetElm(currentStepInfo.value.element);
        if (!currentHighlightLayerElm.value) return;
        scrollToParentVisibleArea(currentHighlightLayerElm.value);
        setHighlightLayerPosition(highlightLayerRef.value);
        setHighlightLayerPosition(referenceLayerRef.value);
        scrollToElm(currentHighlightLayerElm.value);
      });
    };
    var destroyTooltipElm = function destroyTooltipElm() {
      var _referenceLayerRef$va;
      (_referenceLayerRef$va = referenceLayerRef.value) === null || _referenceLayerRef$va === void 0 ? void 0 : _referenceLayerRef$va.parentNode.removeChild(referenceLayerRef.value);
    };
    var showDialogGuide = function showDialogGuide() {
      nextTick(function () {
        currentHighlightLayerElm.value = dialogTooltipRef.value;
        scrollToParentVisibleArea(currentHighlightLayerElm.value);
        setHighlightLayerPosition(highlightLayerRef.value);
        scrollToElm(currentHighlightLayerElm.value);
      });
    };
    var destroyDialogTooltipElm = function destroyDialogTooltipElm() {
      var _dialogTooltipRef$val, _dialogWrapperRef$val;
      (_dialogTooltipRef$val = dialogTooltipRef.value) === null || _dialogTooltipRef$val === void 0 ? void 0 : _dialogTooltipRef$val.parentNode.removeChild(dialogTooltipRef.value);
      (_dialogWrapperRef$val = dialogWrapperRef.value) === null || _dialogWrapperRef$val === void 0 ? void 0 : _dialogWrapperRef$val.parentNode.removeChild(dialogWrapperRef.value);
    };
    var showGuide = function showGuide() {
      if (isPopup.value) {
        destroyDialogTooltipElm();
        showPopupGuide();
      } else {
        destroyTooltipElm();
        showDialogGuide();
      }
    };
    var destroyGuide = function destroyGuide() {
      var _highlightLayerRef$va, _overlayLayerRef$valu;
      destroyTooltipElm();
      destroyDialogTooltipElm();
      (_highlightLayerRef$va = highlightLayerRef.value) === null || _highlightLayerRef$va === void 0 ? void 0 : _highlightLayerRef$va.parentNode.removeChild(highlightLayerRef.value);
      (_overlayLayerRef$valu = overlayLayerRef.value) === null || _overlayLayerRef$valu === void 0 ? void 0 : _overlayLayerRef$valu.parentNode.removeChild(overlayLayerRef.value);
      removeClass(document.body, LOCK_CLASS.value);
    };
    var handleSkip = function handleSkip(e) {
      var _props2$onSkip;
      var total = stepsTotal.value;
      actived.value = false;
      setInnerCurrent(-1, {
        e: e,
        total: total
      });
      (_props2$onSkip = props2.onSkip) === null || _props2$onSkip === void 0 ? void 0 : _props2$onSkip.call(props2, {
        e: e,
        current: innerCurrent.value,
        total: total
      });
    };
    var handlePrev = function handlePrev(e) {
      var _props2$onPrevStepCli;
      var total = stepsTotal.value;
      setInnerCurrent(innerCurrent.value - 1, {
        e: e,
        total: total
      });
      (_props2$onPrevStepCli = props2.onPrevStepClick) === null || _props2$onPrevStepCli === void 0 ? void 0 : _props2$onPrevStepCli.call(props2, {
        e: e,
        prev: innerCurrent.value - 1,
        current: innerCurrent.value,
        total: total
      });
    };
    var handleNext = function handleNext(e) {
      var _props2$onNextStepCli;
      var total = stepsTotal.value;
      setInnerCurrent(innerCurrent.value + 1, {
        e: e,
        total: total
      });
      (_props2$onNextStepCli = props2.onNextStepClick) === null || _props2$onNextStepCli === void 0 ? void 0 : _props2$onNextStepCli.call(props2, {
        e: e,
        next: innerCurrent.value + 1,
        current: innerCurrent.value,
        total: total
      });
    };
    var handleFinish = function handleFinish(e) {
      var _props2$onFinish;
      var total = stepsTotal.value;
      actived.value = false;
      setInnerCurrent(-1, {
        e: e,
        total: total
      });
      (_props2$onFinish = props2.onFinish) === null || _props2$onFinish === void 0 ? void 0 : _props2$onFinish.call(props2, {
        e: e,
        current: innerCurrent.value,
        total: total
      });
    };
    var initGuide = function initGuide() {
      if (innerCurrent.value >= 0 && innerCurrent.value < steps.value.length) {
        if (!actived.value) {
          actived.value = true;
          addClass(document.body, LOCK_CLASS.value);
        }
        showGuide();
      }
    };
    watch(innerCurrent, function (val) {
      if (val >= 0 && val < steps.value.length) {
        initGuide();
      } else {
        actived.value = false;
        destroyGuide();
      }
    });
    onMounted(function () {
      initGuide();
    });
    return function () {
      var renderOverlayLayer = function renderOverlayLayer() {
        return createVNode("div", {
          "ref": overlayLayerRef,
          "class": "".concat(COMPONENT_NAME.value, "__overlay"),
          "style": {
            zIndex: zIndex.value - 2
          }
        }, null);
      };
      var hWithParams = function hWithParams() {
        var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
          currentStepInfo: currentStepInfo.value
        };
        var newH = new Function("return ".concat(h.toString()))();
        return Object.assign({}, newH, params);
      };
      var getHighlightContent = function getHighlightContent() {
        var highlightContent = currentStepInfo.value.highlightContent;
        var node = highlightContent;
        if (isFunction(highlightContent)) {
          node = highlightContent(hWithParams());
        } else if (context.slots.highlightContent) {
          node = context.slots.highlightContent(hWithParams());
        } else if (context.slots["highlight-content"]) {
          node = context.slots["highlight-content"](hWithParams());
        } else if (highlightContent) {
          node = createVNode(node, null, null);
        }
        if (node) {
          if (!node.props) node.props = {};
          node.props["class"] = "".concat(COMPONENT_NAME.value, "__highlight--mask ").concat(node.props["class"] || "");
        }
        return node;
      };
      var renderHighlightLayer = function renderHighlightLayer() {
        var style = {
          zIndex: zIndex.value - 1
        };
        var highlightClass = ["".concat(COMPONENT_NAME.value, "__highlight"), "".concat(COMPONENT_NAME.value, "__highlight--").concat(isPopup.value ? "popup" : "dialog"), "".concat(COMPONENT_NAME.value, "--").concat(currentElmIsFixed.value && isPopup.value ? "fixed" : "absolute")];
        var showOverlay = getCurrentCrossProps("showOverlay");
        var maskClass = ["".concat(COMPONENT_NAME.value, "__highlight--").concat(showOverlay ? "mask" : "nomask")];
        var innerHighlightContent = getHighlightContent();
        var showHighlightContent = Boolean(innerHighlightContent && isPopup.value);
        return createVNode("div", {
          "ref": highlightLayerRef,
          "class": highlightClass.concat(showHighlightContent ? highlightClass : maskClass),
          "style": style
        }, [showHighlightContent && innerHighlightContent]);
      };
      var renderCounter = function renderCounter() {
        var popupSlotCounter = renderTNodeJSX("counter", {
          params: {
            total: stepsTotal.value,
            current: innerCurrent.value
          }
        });
        var popupDefaultCounter = createVNode("div", {
          "class": "".concat(COMPONENT_NAME.value, "__counter")
        }, [popupSlotCounter || "".concat(innerCurrent.value + 1, "/").concat(stepsTotal.value)]);
        return createVNode(Fragment, null, [!hideCounter.value && popupDefaultCounter]);
      };
      var renderAction = function renderAction(mode) {
        var _getCurrentCrossProps, _getCurrentCrossProps2, _getCurrentCrossProps3, _props2$finishButtonP;
        var isLast = innerCurrent.value === stepsTotal.value - 1;
        var isFirst = innerCurrent.value === 0;
        var buttonSize = mode === "popup" ? "small" : "medium";
        return createVNode("div", {
          "class": "".concat(COMPONENT_NAME.value, "__action")
        }, [!hideSkip.value && !isLast && createVNode(Button, mergeProps({
          "class": "".concat(COMPONENT_NAME.value, "__skip"),
          "theme": "default",
          "size": buttonSize,
          "variant": "base",
          "onClick": handleSkip
        }, (_getCurrentCrossProps = getCurrentCrossProps("skipButtonProps")) !== null && _getCurrentCrossProps !== void 0 ? _getCurrentCrossProps : globalConfig.value.skipButtonProps), null), !hidePrev.value && !isFirst && createVNode(Button, mergeProps({
          "class": "".concat(COMPONENT_NAME.value, "__prev"),
          "theme": "primary",
          "size": buttonSize,
          "variant": "base",
          "onClick": handlePrev
        }, (_getCurrentCrossProps2 = getCurrentCrossProps("prevButtonProps")) !== null && _getCurrentCrossProps2 !== void 0 ? _getCurrentCrossProps2 : globalConfig.value.prevButtonProps), null), !isLast && createVNode(Button, mergeProps({
          "class": "".concat(COMPONENT_NAME.value, "__next"),
          "theme": "primary",
          "size": buttonSize,
          "variant": "base",
          "onClick": handleNext
        }, (_getCurrentCrossProps3 = getCurrentCrossProps("nextButtonProps")) !== null && _getCurrentCrossProps3 !== void 0 ? _getCurrentCrossProps3 : globalConfig.value.nextButtonProps), null), isLast && createVNode(Button, mergeProps({
          "class": "".concat(COMPONENT_NAME.value, "__finish"),
          "theme": "primary",
          "size": buttonSize,
          "variant": "base",
          "onClick": handleFinish
        }, (_props2$finishButtonP = props2.finishButtonProps) !== null && _props2$finishButtonP !== void 0 ? _props2$finishButtonP : globalConfig.value.finishButtonProps), null)]);
      };
      var renderTitle = function renderTitle() {
        var functionTitle = isFunction(currentStepInfo.value.title) ? currentStepInfo.value.title() : void 0;
        var slotTitle = context.slots.title ? context.slots.title(hWithParams()) : void 0;
        return functionTitle || slotTitle || currentStepInfo.value.title;
      };
      var renderTooltipBody = function renderTooltipBody() {
        var title = createVNode("div", {
          "class": "".concat(COMPONENT_NAME.value, "__title")
        }, [renderTitle()]);
        var bodyRender = currentStepInfo.value.body;
        var descBody;
        if (isFunction(bodyRender)) descBody = bodyRender(hWithParams());else if (context.slots.body) descBody = context.slots.body({
          currentStepInfo: currentStepInfo.value
        });else if (typeof bodyRender === "string") descBody = bodyRender;else descBody = createVNode(bodyRender, null, null);
        var desc = createVNode("div", {
          "class": "".concat(COMPONENT_NAME.value, "__desc")
        }, [descBody]);
        return createVNode(Fragment, null, [title, desc]);
      };
      var renderPopupContent = function renderPopupContent() {
        var footerClasses = ["".concat(COMPONENT_NAME.value, "__footer"), "".concat(COMPONENT_NAME.value, "__footer--popup")];
        var action = createVNode("div", {
          "class": footerClasses
        }, [renderCounter(), renderAction("popup")]);
        return createVNode("div", {
          "class": "".concat(COMPONENT_NAME.value, "__tooltip")
        }, [renderTooltipBody(), action]);
      };
      var renderPopupGuide = function renderPopupGuide() {
        var _currentStepInfo$valu2;
        var content = currentStepInfo.value.content;
        var contentProps = {
          handlePrev: handlePrev,
          handleNext: handleNext,
          handleSkip: handleSkip,
          handleFinish: handleFinish,
          current: innerCurrent.value,
          total: stepsTotal.value
        };
        var renderBody;
        if (isFunction(content)) renderBody = function renderBody() {
          return content(hWithParams(contentProps));
        };else if (context.slots.content) renderBody = function renderBody() {
          return context.slots.content(hWithParams(contentProps));
        };else if (content) renderBody = function renderBody() {
          return createVNode(content, contentProps, null);
        };else renderBody = renderPopupContent;
        var classes = ["".concat(COMPONENT_NAME.value, "__reference"), "".concat(COMPONENT_NAME.value, "--").concat(currentElmIsFixed.value ? "fixed" : "absolute")];
        var innerClassName = [_defineProperty({}, "".concat(COMPONENT_NAME.value, "__popup--content"), !!content)];
        return createVNode(Popup, mergeProps({
          "visible": true,
          "show-arrow": !content,
          "zIndex": zIndex.value,
          "placement": currentStepInfo.value.placement
        }, currentStepInfo.value.popupProps, {
          "content": renderBody,
          "overlayClassName": ["".concat(COMPONENT_NAME.value, "__popup"), currentStepInfo.value.stepOverlayClass],
          "overlayInnerClassName": innerClassName.concat((_currentStepInfo$valu2 = currentStepInfo.value.popupProps) === null || _currentStepInfo$valu2 === void 0 ? void 0 : _currentStepInfo$valu2.overlayInnerClassName)
        }), {
          "default": function _default() {
            return [createVNode("div", {
              "ref": referenceLayerRef,
              "class": classes
            }, null)];
          }
        });
      };
      var renderDialogGuide = function renderDialogGuide() {
        var _ref3;
        var style = {
          zIndex: zIndex.value
        };
        var wrapperClasses = ["".concat(COMPONENT_NAME.value, "__wrapper"), _defineProperty({}, "".concat(COMPONENT_NAME.value, "__wrapper--center"), currentStepInfo.value.placement === "center")];
        var dialogClasses = ["".concat(COMPONENT_NAME.value, "__reference"), "".concat(COMPONENT_NAME.value, "--absolute"), "".concat(COMPONENT_NAME.value, "__dialog"), (_ref3 = {}, _defineProperty(_ref3, "".concat(COMPONENT_NAME.value, "__dialog--nomask"), !getCurrentCrossProps("showOverlay")), _defineProperty(_ref3, currentStepInfo.value.stepOverlayClass, !!currentStepInfo.value.stepOverlayClass), _ref3)];
        var footerClasses = ["".concat(COMPONENT_NAME.value, "__footer"), "".concat(COMPONENT_NAME.value, "__footer--popup")];
        return createVNode(Fragment, null, [createVNode("div", {
          "ref": dialogWrapperRef,
          "class": wrapperClasses,
          "style": style
        }, [createVNode("div", {
          "ref": dialogTooltipRef,
          "class": dialogClasses
        }, [renderTooltipBody(), createVNode("div", {
          "class": footerClasses
        }, [renderCounter(), renderAction("dialog")])])])]);
      };
      var renderGuide = function renderGuide() {
        return createVNode(Fragment, null, [createVNode(Teleport, {
          "to": "body"
        }, {
          "default": function _default() {
            return [renderOverlayLayer(), renderHighlightLayer(), isPopup.value ? renderPopupGuide() : renderDialogGuide()];
          }
        })]);
      };
      return createVNode(Fragment, null, [actived.value && renderGuide()]);
    };
  }
});

export { _Guide as default };
//# sourceMappingURL=guide.js.map
