import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
import { _ as _define_property } from "@swc/helpers/_/_define_property";
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
import { _ as _object_without_properties } from "@swc/helpers/_/_object_without_properties";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
import React, { useEffect, useState } from "react";
import { View } from "@tarojs/components";
import classNames from "classnames";
import { Loading1 } from "@nutui/icons-react-taro";
import { ComponentDefaults } from "../../utils/typings";
import { usePropsValue } from "../../hooks/use-props-value";
import { useRtl } from "../configprovider/index";
var defaultProps = _object_spread_props(_object_spread({}, ComponentDefaults), {
    disabled: false,
    activeText: '',
    inactiveText: '',
    loadingIcon: /*#__PURE__*/ React.createElement(Loading1, null),
    loading: undefined,
    onLoadingChange: function(loading) {}
});
export var Switch = function(props) {
    var _ref = _object_spread({}, defaultProps, props), checked = _ref.checked, defaultChecked = _ref.defaultChecked, disabled = _ref.disabled, activeText = _ref.activeText, inactiveText = _ref.inactiveText, loadingIcon = _ref.loadingIcon, className = _ref.className, style = _ref.style, onChange = _ref.onChange, propLoading = _ref.loading, onLoadingChange = _ref.onLoadingChange, ariaLabel = _ref.ariaLabel, rest = _object_without_properties(_ref, [
        "checked",
        "defaultChecked",
        "disabled",
        "activeText",
        "inactiveText",
        "loadingIcon",
        "className",
        "style",
        "onChange",
        "loading",
        "onLoadingChange",
        "ariaLabel"
    ]);
    var classPrefix = 'nut-switch';
    var rtl = useRtl();
    var _usePropsValue = _sliced_to_array(usePropsValue({
        value: checked,
        defaultValue: defaultChecked
    }), 2), value = _usePropsValue[0], setValue = _usePropsValue[1];
    var _useState = _sliced_to_array(useState(false), 2), internalLoading = _useState[0], setInternalLoading = _useState[1];
    var loading = propLoading !== undefined ? propLoading : internalLoading;
    var setLoading = function(val) {
        if (propLoading !== undefined) {
            onLoadingChange(val);
        } else {
            setInternalLoading(val);
        }
    };
    useEffect(function() {
        loading && setLoading(false);
    }, [
        value
    ]);
    var classes = function() {
        var _obj;
        return classNames([
            classPrefix,
            className,
            (_obj = {}, _define_property(_obj, "".concat(classPrefix, "-close"), !value), _define_property(_obj, "".concat(classPrefix, "-disabled"), disabled), _define_property(_obj, "".concat(classPrefix, "-disabled-close"), disabled && !value), _obj)
        ]);
    };
    var onClick = function() {
        return _async_to_generator(function() {
            var e;
            return _ts_generator(this, function(_state) {
                switch(_state.label){
                    case 0:
                        if (disabled || loading) return [
                            2
                        ];
                        if (!onChange) return [
                            3,
                            4
                        ];
                        loadingIcon && setLoading(true);
                        _state.label = 1;
                    case 1:
                        _state.trys.push([
                            1,
                            3,
                            ,
                            4
                        ]);
                        return [
                            4,
                            onChange(!value)
                        ];
                    case 2:
                        _state.sent();
                        return [
                            3,
                            4
                        ];
                    case 3:
                        e = _state.sent();
                        setLoading(false);
                        return [
                            3,
                            4
                        ];
                    case 4:
                        setValue(!value);
                        return [
                            2
                        ];
                }
            });
        })();
    };
    var _obj, _obj1;
    return /*#__PURE__*/ React.createElement(View, _object_spread({
        className: classes(),
        onClick: onClick,
        style: style,
        ariaRole: "switch",
        // @ts-ignore
        ariaChecked: value,
        ariaDisabled: disabled,
        tabIndex: disabled ? -1 : 0,
        ariaLabel: ariaLabel
    }, rest), /*#__PURE__*/ React.createElement(View, {
        className: classNames([
            [
                "".concat(classPrefix, "-button")
            ],
            [
                value ? "".concat(classPrefix, "-button-open") : "".concat(classPrefix, "-button-close")
            ],
            (_obj = {}, _define_property(_obj, "".concat(classPrefix, "-button-open-rtl"), rtl && value), _define_property(_obj, "".concat(classPrefix, "-button-close-rtl"), rtl && !value), _obj)
        ])
    }, loading && loadingIcon ? /*#__PURE__*/ React.createElement(React.Fragment, null, loadingIcon) : /*#__PURE__*/ React.createElement(React.Fragment, null, !value && !activeText && /*#__PURE__*/ React.createElement(View, {
        className: "".concat(classPrefix, "-close-line")
    }))), activeText && /*#__PURE__*/ React.createElement(View, {
        className: classNames("".concat(classPrefix, "-label"), (_obj1 = {}, _define_property(_obj1, "".concat(classPrefix, "-label-open"), value), _define_property(_obj1, "".concat(classPrefix, "-label-close"), !value), _define_property(_obj1, "".concat(classPrefix, "-label-open-rtl"), rtl && value), _define_property(_obj1, "".concat(classPrefix, "-label-close-rtl"), rtl && !value), _define_property(_obj1, "".concat(classPrefix, "-label-close-disabled"), disabled && !value), _obj1))
    }, value ? activeText : inactiveText));
};
Switch.displayName = 'NutSwitch';
