"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "Swiper", {
    enumerable: true,
    get: function() {
        return Swiper;
    }
});
var _interop_require_default = require("@swc/helpers/_/_interop_require_default");
var _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
var _define_property = require("@swc/helpers/_/_define_property");
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 _components = require("@tarojs/components");
var _classnames = /*#__PURE__*/ _interop_require_default._(require("classnames"));
var _index = /*#__PURE__*/ _interop_require_default._(require("../indicator/index"));
var _pxtransform = require("../../utils/taro/px-transform");
var defaultProps = {
    direction: 'horizontal',
    indicator: false,
    autoPlay: false,
    autoplay: false,
    loop: false,
    defaultValue: 0,
    style: {}
};
var classPrefix = 'nut-swiper';
var Swiper = /*#__PURE__*/ _react.default.forwardRef(function(props, ref) {
    var _ref = (0, _object_spread._)({}, defaultProps, props), width = _ref.width, height = _ref.height, className = _ref.className, children = _ref.children, indicator = _ref.indicator, indicatorDots = _ref.indicatorDots, loop = _ref.loop, circular = _ref.circular, autoPlay = _ref.autoPlay, autoplay = _ref.autoplay, vertical = _ref.vertical, direction = _ref.direction, defaultValue = _ref.defaultValue, current = _ref.current, onChange = _ref.onChange, style = _ref.style, rest = (0, _object_without_properties._)(_ref, [
        "width",
        "height",
        "className",
        "children",
        "indicator",
        "indicatorDots",
        "loop",
        "circular",
        "autoPlay",
        "autoplay",
        "vertical",
        "direction",
        "defaultValue",
        "current",
        "onChange",
        "style"
    ]);
    var _useState = (0, _sliced_to_array._)((0, _react.useState)(current || defaultValue), 2), innerValue = _useState[0], setInnerValue = _useState[1];
    var childrenCount = (0, _react.useMemo)(function() {
        var c = 0;
        _react.default.Children.map(children, function(child, index) {
            c += 1;
        });
        return c;
    }, [
        children
    ]);
    (0, _react.useEffect)(function() {
        setInnerValue(current || defaultValue);
    }, [
        defaultValue,
        current
    ]);
    var renderIndicator = function renderIndicator() {
        if (/*#__PURE__*/ _react.default.isValidElement(indicator)) return indicator;
        if (indicator || indicatorDots) {
            var _obj;
            return /*#__PURE__*/ _react.default.createElement(_components.View, {
                className: (0, _classnames.default)((_obj = {}, (0, _define_property._)(_obj, "".concat(classPrefix, "-indicator"), true), (0, _define_property._)(_obj, "".concat(classPrefix, "-indicator-vertical"), direction === 'vertical' || vertical), _obj))
            }, /*#__PURE__*/ _react.default.createElement(_index.default, {
                current: innerValue,
                total: childrenCount,
                direction: vertical ? 'vertical' : direction
            }));
        }
        return null;
    };
    var handleOnChange = function handleOnChange(value) {
        setInnerValue(value.detail.current);
    };
    (0, _react.useImperativeHandle)(ref, function() {
        return {
            to: function to(value) {
                setInnerValue(value);
            },
            next: function next() {
                if (loop) {
                    setInnerValue((innerValue + 1) % childrenCount);
                } else {
                    setInnerValue(innerValue + 1 >= childrenCount ? innerValue : innerValue + 1);
                }
            },
            prev: function prev() {
                if (loop) {
                    var next = innerValue - 1;
                    next = next < 0 ? childrenCount + next : next;
                    setInnerValue(next % childrenCount);
                } else {
                    setInnerValue(innerValue - 1 <= 0 ? 0 : innerValue - 1);
                }
            }
        };
    });
    return /*#__PURE__*/ _react.default.createElement(_components.View, {
        className: (0, _classnames.default)(classPrefix, className),
        style: (0, _object_spread_props._)((0, _object_spread._)({}, style), {
            width: !width ? '100%' : width,
            height: !height ? (0, _pxtransform.pxTransform)(150) : height
        })
    }, /*#__PURE__*/ _react.default.createElement(_components.View, {
        className: "nut-swiper-inner",
        style: {
            width: !width ? '100%' : width,
            height: !height ? (0, _pxtransform.pxTransform)(150) : height
        }
    }, /*#__PURE__*/ _react.default.createElement(_components.Swiper, (0, _object_spread._)({
        current: innerValue,
        circular: loop,
        autoplay: autoplay || autoPlay,
        vertical: direction === 'vertical' || vertical,
        indicatorDots: false,
        onChange: function onChange(e) {
            var _props_onChange;
            handleOnChange(e);
            (_props_onChange = props.onChange) === null || _props_onChange === void 0 ? void 0 : _props_onChange.call(props, e);
        },
        style: {
            width: !width ? '100%' : width,
            height: !height ? (0, _pxtransform.pxTransform)(150) : height
        }
    }, rest), _react.Children.toArray(children).map(function(child, index) {
        var className;
        if (/*#__PURE__*/ _react.default.isValidElement(child)) {
            className = child.props.className;
        }
        return /*#__PURE__*/ _react.default.createElement(_components.SwiperItem, {
            className: className,
            key: index
        }, child);
    }))), renderIndicator());
});
Swiper.displayName = 'NutSwiper';
