"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 _sliced_to_array = require("@swc/helpers/_/_sliced_to_array");
var _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
var _iconsreacttaro = require("@nutui/icons-react-taro");
var _classnames = /*#__PURE__*/ _interop_require_default._(require("classnames"));
var _components = require("@tarojs/components");
var _index = /*#__PURE__*/ _interop_require_default._(require("../popover/index"));
var _getrectbyid = require("../../utils/taro/get-rect-by-id");
var _typings = require("../../utils/typings");
var _index1 = require("../configprovider/index");
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, _index1.useConfig)().locale;
    var _$_object_spread = (0, _object_spread._)({}, defaultProps, props), children = _$_object_spread.children, className = _$_object_spread.className, title = _$_object_spread.title, closeOnOverlayClick = _$_object_spread.closeOnOverlayClick, showPrev = _$_object_spread.showPrev, list = _$_object_spread.list, type = _$_object_spread.type, location = _$_object_spread.location, visible = _$_object_spread.visible, mask = _$_object_spread.mask, maskWidth = _$_object_spread.maskWidth, maskHeight = _$_object_spread.maskHeight, offset = _$_object_spread.offset, next = _$_object_spread.next, prev = _$_object_spread.prev, complete = _$_object_spread.complete, onClose = _$_object_spread.onClose, onChange = _$_object_spread.onChange, style = _$_object_spread.style;
    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() {
        (0, _getrectbyid.getRectById)(list[active].target).then(function(rect) {
            setMaskRect({
                top: rect.top,
                left: rect.left,
                right: rect.right,
                bottom: rect.bottom,
                width: rect.width,
                height: rect.height
            });
        });
    };
    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(_components.View, {
        className: classes,
        style: style
    }, /*#__PURE__*/ _react.default.createElement(_components.View, {
        className: "nut-tour-masked",
        style: {
            display: showTour ? 'block' : 'none'
        },
        onClick: handleClickMask
    }), list.map(function(item, index) {
        return /*#__PURE__*/ _react.default.createElement(_components.View, {
            key: index,
            style: {
                height: 0
            }
        }, index === active && /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, showTour && /*#__PURE__*/ _react.default.createElement(_components.View, {
            className: "".concat(mask ? 'nut-tour-mask' : 'nut-tour-mask nut-tour-mask-none'),
            id: "nut-tour-popid".concat(index),
            style: maskStyle()
        }), /*#__PURE__*/ _react.default.createElement(_index.default, {
            visible: showPopup,
            location: item.location || location,
            targetId: "nut-tour-popid".concat(index === active ? index : ''),
            closeOnOutsideClick: false,
            offset: item.popoverOffset || [
                0,
                8
            ],
            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(_components.View, {
            className: "nut-tour-content"
        }, title && /*#__PURE__*/ _react.default.createElement(_components.View, {
            className: "nut-tour-content-top"
        }, /*#__PURE__*/ _react.default.createElement(_components.View, {
            onClick: function onClick(e) {
                return maskClose(e);
            }
        }, /*#__PURE__*/ _react.default.createElement(_iconsreacttaro.Close, {
            className: "nut-tour-content-top-close"
        }))), /*#__PURE__*/ _react.default.createElement(_components.View, {
            className: "nut-tour-content-inner"
        }, item.content), /*#__PURE__*/ _react.default.createElement(_components.View, {
            className: "nut-tour-content-bottom"
        }, /*#__PURE__*/ _react.default.createElement(_components.View, {
            className: "nut-tour-content-bottom-init"
        }, active + 1, "/", list.length), /*#__PURE__*/ _react.default.createElement(_components.View, {
            className: "nut-tour-content-bottom-operate"
        }, active !== 0 && showPrev && /*#__PURE__*/ _react.default.createElement(_components.View, {
            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(_components.View, {
            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(_components.View, {
            className: "nut-tour-content-bottom-operate-btn active",
            onClick: function onClick() {
                return changeStep('next');
            }
        }, next || locale.tour.nextStepText)))), type === 'tile' && /*#__PURE__*/ _react.default.createElement(_components.View, {
            className: "nut-tour-content nut-tour-content-tile"
        }, /*#__PURE__*/ _react.default.createElement(_components.View, {
            className: "nut-tour-content-inner"
        }, item.content)))))));
    }));
};
Tour.displayName = 'NutTour';
