"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "Tour", {
    enumerable: true,
    get: function() {
        return Tour;
    }
});
var _interop_require_default = require("@swc/helpers/_/_interop_require_default");
var _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
var _object_spread = require("@swc/helpers/_/_object_spread");
var _object_spread_props = require("@swc/helpers/_/_object_spread_props");
var _object_without_properties = require("@swc/helpers/_/_object_without_properties");
var _sliced_to_array = require("@swc/helpers/_/_sliced_to_array");
var _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
var _iconsreact = require("@nutui/icons-react");
var _classnames = /*#__PURE__*/ _interop_require_default._(require("classnames"));
var _popover = /*#__PURE__*/ _interop_require_default._(require("../popover"));
var _useclientrect = require("../../hooks/use-client-rect");
var _typings = require("../../utils/typings");
var _configprovider = require("../configprovider");
var defaultProps = (0, _object_spread_props._)((0, _object_spread._)({}, _typings.ComponentDefaults), {
    visible: false,
    type: 'step',
    location: 'bottom',
    mask: true,
    maskWidth: '',
    maskHeight: '',
    offset: [
        8,
        10
    ],
    title: '',
    next: '',
    prev: '',
    complete: '',
    showPrev: true,
    closeOnOverlayClick: true
});
var classPrefix = 'nut-tour';
var Tour = function Tour(props) {
    var locale = (0, _configprovider.useConfig)().locale;
    var _ref = (0, _object_spread._)({}, defaultProps, props), children = _ref.children, className = _ref.className, title = _ref.title, closeOnOverlayClick = _ref.closeOnOverlayClick, showPrev = _ref.showPrev, list = _ref.list, type = _ref.type, location = _ref.location, visible = _ref.visible, mask = _ref.mask, maskWidth = _ref.maskWidth, maskHeight = _ref.maskHeight, offset = _ref.offset, next = _ref.next, prev = _ref.prev, complete = _ref.complete, onClose = _ref.onClose, onChange = _ref.onChange, rest = (0, _object_without_properties._)(_ref, [
        "children",
        "className",
        "title",
        "closeOnOverlayClick",
        "showPrev",
        "list",
        "type",
        "location",
        "visible",
        "mask",
        "maskWidth",
        "maskHeight",
        "offset",
        "next",
        "prev",
        "complete",
        "onClose",
        "onChange"
    ]);
    var _useState = (0, _sliced_to_array._)((0, _react.useState)(false), 2), showTour = _useState[0], setShowTour = _useState[1];
    var _useState1 = (0, _sliced_to_array._)((0, _react.useState)(false), 2), showPopup = _useState1[0], setShowPopup = _useState1[1];
    var _useState2 = (0, _sliced_to_array._)((0, _react.useState)(0), 2), active = _useState2[0], setActive = _useState2[1];
    var _useState3 = (0, _sliced_to_array._)((0, _react.useState)({
        top: 0,
        left: 0,
        right: 0,
        bottom: 0,
        width: 0,
        height: 0
    }), 2), maskRect = _useState3[0], setMaskRect = _useState3[1];
    var classes = (0, _classnames.default)(classPrefix, className);
    (0, _react.useEffect)(function() {
        if (visible) {
            getRootPosition();
        }
        setActive(0);
        setShowTour(visible);
        setShowPopup(visible);
    }, [
        visible
    ]);
    (0, _react.useEffect)(function() {
        if (visible) {
            setShowPopup(true);
            getRootPosition();
        }
    }, [
        active
    ]);
    var getRootPosition = function getRootPosition() {
        var el = document.querySelector("#".concat(list[active].target));
        var rect = (0, _useclientrect.getRect)(el);
        setMaskRect(rect);
    };
    var maskStyle = function maskStyle() {
        var width = maskRect.width, height = maskRect.height, left = maskRect.left, top = maskRect.top;
        var center = [
            left + width / 2,
            top + height / 2
        ] // 中心点 【横，纵】
        ;
        var w = Number(maskWidth || width);
        var h = Number(maskHeight || height);
        var styles = {
            width: "".concat(w + +offset[1] * 2, "px"),
            height: "".concat(h + +offset[0] * 2, "px"),
            top: "".concat(center[1] - h / 2 - +offset[0], "px"),
            left: "".concat(center[0] - w / 2 - +offset[1], "px")
        };
        return styles;
    };
    var maskClose = function maskClose(e) {
        setShowTour(false);
        setShowPopup(false);
        onClose && onClose(e);
    };
    var handleClickMask = function handleClickMask(e) {
        closeOnOverlayClick && maskClose(e);
    };
    var changeStep = function changeStep(type) {
        if (type === 'next') {
            setActive(active + 1);
            onChange && onChange(active + 1);
        } else {
            setActive(active - 1);
            onChange && onChange(active - 1);
        }
        setShowPopup(false);
    };
    return /*#__PURE__*/ _react.default.createElement("div", (0, _object_spread._)({
        className: classes
    }, rest), /*#__PURE__*/ _react.default.createElement("div", {
        className: "nut-tour-masked",
        style: {
            display: showTour ? 'block' : 'none'
        },
        onClick: handleClickMask
    }), list.map(function(item, index) {
        return /*#__PURE__*/ _react.default.createElement("div", {
            key: index,
            style: {
                height: 0
            }
        }, index === active && /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, showTour && /*#__PURE__*/ _react.default.createElement("div", {
            className: "".concat(mask ? 'nut-tour-mask' : 'nut-tour-mask nut-tour-mask-none'),
            id: "nut-tour-popid",
            style: maskStyle()
        }), /*#__PURE__*/ _react.default.createElement(_popover.default, {
            visible: showPopup,
            location: item.location || location,
            targetId: "nut-tour-popid",
            closeOnOutsideClick: false,
            offset: item.popoverOffset || [
                0,
                12
            ],
            arrowOffset: item.arrowOffset || 0
        }, /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null), /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, children || /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, type === 'step' && /*#__PURE__*/ _react.default.createElement("div", {
            className: "nut-tour-content"
        }, title && /*#__PURE__*/ _react.default.createElement("div", {
            className: "nut-tour-content-top"
        }, /*#__PURE__*/ _react.default.createElement("div", {
            onClick: function onClick(e) {
                return maskClose(e);
            }
        }, /*#__PURE__*/ _react.default.createElement(_iconsreact.Close, {
            className: "nut-tour-content-top-close"
        }))), /*#__PURE__*/ _react.default.createElement("div", {
            className: "nut-tour-content-inner"
        }, item.content), /*#__PURE__*/ _react.default.createElement("div", {
            className: "nut-tour-content-bottom"
        }, /*#__PURE__*/ _react.default.createElement("div", {
            className: "nut-tour-content-bottom-init"
        }, active + 1, "/", list.length), /*#__PURE__*/ _react.default.createElement("div", {
            className: "nut-tour-content-bottom-operate"
        }, active !== 0 && showPrev && /*#__PURE__*/ _react.default.createElement("div", {
            className: "nut-tour-content-bottom-operate-btn",
            onClick: function onClick() {
                return changeStep('prev');
            }
        }, prev || locale.tour.prevStepText), list.length - 1 === active && /*#__PURE__*/ _react.default.createElement("div", {
            className: "nut-tour-content-bottom-operate-btn active",
            onClick: function onClick(e) {
                return maskClose(e);
            }
        }, complete || locale.tour.completeText), list.length - 1 !== active && /*#__PURE__*/ _react.default.createElement("div", {
            className: "nut-tour-content-bottom-operate-btn active",
            onClick: function onClick() {
                return changeStep('next');
            }
        }, next || locale.tour.nextStepText)))), type === 'tile' && /*#__PURE__*/ _react.default.createElement("div", {
            className: "nut-tour-content nut-tour-content-tile"
        }, /*#__PURE__*/ _react.default.createElement("div", {
            className: "nut-tour-content-inner"
        }, item.content)))))));
    }));
};
Tour.displayName = 'NutTour';
