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

'use strict';

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

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var vue = require('vue');
var isFunction = require('lodash/isFunction');
var _common_js_utils_setStyle = require('../_common/js/utils/set-style.js');
var utils_dom = require('../utils/dom.js');
var hooks_useVModel = require('../hooks/useVModel.js');
var hooks_tnode = require('../hooks/tnode.js');
var hooks_useConfig = require('../hooks/useConfig.js');
var button_index = require('../button/index.js');
var popup_index = require('../popup/index.js');
var guide_utils_getScrollParent = require('./utils/getScrollParent.js');
var guide_utils_getRelativePosition = require('./utils/getRelativePosition.js');
var guide_utils_getTargetElm = require('./utils/getTargetElm.js');
var guide_utils_scrollToElm = require('./utils/scrollToElm.js');
var guide_props = require('./props.js');
var configProvider_useConfig = require('../config-provider/useConfig.js');
require('../_chunks/dep-f4db2590.js');
require('lodash/isString');
require('lodash/isArray');
require('../utils/easing.js');
require('lodash/kebabCase');
require('lodash/camelCase');
require('../utils/render-tnode.js');
require('lodash/isEmpty');
require('lodash/isObject');
require('../utils/withInstall.js');
require('../button/button.js');
require('@babel/runtime/helpers/toConsumableArray');
require('../loading/index.js');
require('../loading/directive.js');
require('../loading/plugin.js');
require('../loading/loading.js');
require('../hooks/useTeleport.js');
require('../loading/props.js');
require('../loading/icon/gradient.js');
require('../_common/js/loading/circle-adapter.js');
require('../_common/js/utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('lodash/isNull');
require('lodash/isUndefined');
require('lodash/isNumber');
require('lodash/cloneDeep');
require('../config-provider/context.js');
require('lodash/mergeWith');
require('lodash/merge');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');
require('@babel/runtime/helpers/typeof');
require('dayjs');
require('../hooks/useRipple.js');
require('../utils/set-style.js');
require('../hooks/useKeepAnimation.js');
require('../form/hooks.js');
require('../button/props.js');
require('../popup/popup.js');
require('@popperjs/core');
require('../popup/props.js');
require('../popup/container.js');
require('../hooks/useResizeObserver.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray);
var isFunction__default = /*#__PURE__*/_interopDefaultLegacy(isFunction);

var _Guide = vue.defineComponent({
  name: "TGuide",
  props: guide_props["default"],
  setup: function setup(props2, context) {
    var renderTNodeJSX = hooks_tnode.useTNodeJSX();
    var COMPONENT_NAME = hooks_useConfig.usePrefixClass("guide");
    var LOCK_CLASS = hooks_useConfig.usePrefixClass("guide--lock");
    var _useConfig = configProvider_useConfig.useConfig("guide"),
      globalConfig = _useConfig.globalConfig;
    var _toRefs = vue.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 = hooks_useVModel["default"](current, modelValue, props2.defaultCurrent, props2.onChange, "current"),
      _useVModel2 = _slicedToArray__default["default"](_useVModel, 2),
      innerCurrent = _useVModel2[0],
      setInnerCurrent = _useVModel2[1];
    var overlayLayerRef = vue.ref();
    var highlightLayerRef = vue.ref();
    var referenceLayerRef = vue.ref();
    var currentHighlightLayerElm = vue.ref();
    var dialogWrapperRef = vue.ref();
    var dialogTooltipRef = vue.ref();
    var actived = vue.ref(false);
    var stepsTotal = vue.computed(function () {
      return steps.value.length;
    });
    var currentStepInfo = vue.computed(function () {
      return steps.value[innerCurrent.value];
    });
    var isPopup = vue.computed(function () {
      return getCurrentCrossProps("mode") === "popup";
    });
    var currentElmIsFixed = vue.computed(function () {
      return utils_dom.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 = guide_utils_getRelativePosition["default"](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 = utils_dom.getWindowScroll(),
          scrollTop = _getWindowScroll.scrollTop,
          scrollLeft = _getWindowScroll.scrollLeft;
        top += scrollTop;
        left += scrollLeft;
      }
      _common_js_utils_setStyle["default"](highlighLayer, {
        width: "".concat(width, "px"),
        height: "".concat(height, "px"),
        top: "".concat(top, "px"),
        left: "".concat(left, "px")
      });
    };
    var showPopupGuide = function showPopupGuide() {
      vue.nextTick(function () {
        currentHighlightLayerElm.value = guide_utils_getTargetElm["default"](currentStepInfo.value.element);
        if (!currentHighlightLayerElm.value) return;
        guide_utils_getScrollParent.scrollToParentVisibleArea(currentHighlightLayerElm.value);
        setHighlightLayerPosition(highlightLayerRef.value);
        setHighlightLayerPosition(referenceLayerRef.value);
        guide_utils_scrollToElm["default"](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() {
      vue.nextTick(function () {
        currentHighlightLayerElm.value = dialogTooltipRef.value;
        guide_utils_getScrollParent.scrollToParentVisibleArea(currentHighlightLayerElm.value);
        setHighlightLayerPosition(highlightLayerRef.value);
        guide_utils_scrollToElm["default"](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);
      utils_dom.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;
          utils_dom.addClass(document.body, LOCK_CLASS.value);
        }
        showGuide();
      }
    };
    vue.watch(innerCurrent, function (val) {
      if (val >= 0 && val < steps.value.length) {
        initGuide();
      } else {
        actived.value = false;
        destroyGuide();
      }
    });
    vue.onMounted(function () {
      initGuide();
    });
    return function () {
      var renderOverlayLayer = function renderOverlayLayer() {
        return vue.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(vue.h.toString()))();
        return Object.assign({}, newH, params);
      };
      var getHighlightContent = function getHighlightContent() {
        var highlightContent = currentStepInfo.value.highlightContent;
        var node = highlightContent;
        if (isFunction__default["default"](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 = vue.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 vue.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 = vue.createVNode("div", {
          "class": "".concat(COMPONENT_NAME.value, "__counter")
        }, [popupSlotCounter || "".concat(innerCurrent.value + 1, "/").concat(stepsTotal.value)]);
        return vue.createVNode(vue.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 vue.createVNode("div", {
          "class": "".concat(COMPONENT_NAME.value, "__action")
        }, [!hideSkip.value && !isLast && vue.createVNode(button_index.Button, vue.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 && vue.createVNode(button_index.Button, vue.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 && vue.createVNode(button_index.Button, vue.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 && vue.createVNode(button_index.Button, vue.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__default["default"](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 = vue.createVNode("div", {
          "class": "".concat(COMPONENT_NAME.value, "__title")
        }, [renderTitle()]);
        var bodyRender = currentStepInfo.value.body;
        var descBody;
        if (isFunction__default["default"](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 = vue.createVNode(bodyRender, null, null);
        var desc = vue.createVNode("div", {
          "class": "".concat(COMPONENT_NAME.value, "__desc")
        }, [descBody]);
        return vue.createVNode(vue.Fragment, null, [title, desc]);
      };
      var renderPopupContent = function renderPopupContent() {
        var footerClasses = ["".concat(COMPONENT_NAME.value, "__footer"), "".concat(COMPONENT_NAME.value, "__footer--popup")];
        var action = vue.createVNode("div", {
          "class": footerClasses
        }, [renderCounter(), renderAction("popup")]);
        return vue.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__default["default"](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 vue.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__default["default"]({}, "".concat(COMPONENT_NAME.value, "__popup--content"), !!content)];
        return vue.createVNode(popup_index.Popup, vue.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 [vue.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__default["default"]({}, "".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__default["default"](_ref3, "".concat(COMPONENT_NAME.value, "__dialog--nomask"), !getCurrentCrossProps("showOverlay")), _defineProperty__default["default"](_ref3, currentStepInfo.value.stepOverlayClass, !!currentStepInfo.value.stepOverlayClass), _ref3)];
        var footerClasses = ["".concat(COMPONENT_NAME.value, "__footer"), "".concat(COMPONENT_NAME.value, "__footer--popup")];
        return vue.createVNode(vue.Fragment, null, [vue.createVNode("div", {
          "ref": dialogWrapperRef,
          "class": wrapperClasses,
          "style": style
        }, [vue.createVNode("div", {
          "ref": dialogTooltipRef,
          "class": dialogClasses
        }, [renderTooltipBody(), vue.createVNode("div", {
          "class": footerClasses
        }, [renderCounter(), renderAction("dialog")])])])]);
      };
      var renderGuide = function renderGuide() {
        return vue.createVNode(vue.Fragment, null, [vue.createVNode(vue.Teleport, {
          "to": "body"
        }, {
          "default": function _default() {
            return [renderOverlayLayer(), renderHighlightLayer(), isPopup.value ? renderPopupGuide() : renderDialogGuide()];
          }
        })]);
      };
      return vue.createVNode(vue.Fragment, null, [actived.value && renderGuide()]);
    };
  }
});

exports["default"] = _Guide;
//# sourceMappingURL=guide.js.map
