"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "Switch", {
    enumerable: true,
    get: function() {
        return Switch;
    }
});
var _interop_require_default = require("@swc/helpers/_/_interop_require_default");
var _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
var _async_to_generator = require("@swc/helpers/_/_async_to_generator");
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 _ts_generator = require("@swc/helpers/_/_ts_generator");
var _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
var _components = require("@tarojs/components");
var _classnames = /*#__PURE__*/ _interop_require_default._(require("classnames"));
var _iconsreacttaro = require("@nutui/icons-react-taro");
var _typings = require("../../utils/typings");
var _usepropsvalue = require("../../hooks/use-props-value");
var _index = require("../configprovider/index");
var defaultProps = (0, _object_spread_props._)((0, _object_spread._)({}, _typings.ComponentDefaults), {
    disabled: false,
    activeText: '',
    inactiveText: '',
    loadingIcon: /*#__PURE__*/ _react.default.createElement(_iconsreacttaro.Loading1, null),
    loading: undefined,
    onLoadingChange: function onLoadingChange(loading) {}
});
var Switch = function Switch(props) {
    var _ref = (0, _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 = (0, _object_without_properties._)(_ref, [
        "checked",
        "defaultChecked",
        "disabled",
        "activeText",
        "inactiveText",
        "loadingIcon",
        "className",
        "style",
        "onChange",
        "loading",
        "onLoadingChange",
        "ariaLabel"
    ]);
    var classPrefix = 'nut-switch';
    var rtl = (0, _index.useRtl)();
    var _usePropsValue = (0, _sliced_to_array._)((0, _usepropsvalue.usePropsValue)({
        value: checked,
        defaultValue: defaultChecked
    }), 2), value = _usePropsValue[0], setValue = _usePropsValue[1];
    var _useState = (0, _sliced_to_array._)((0, _react.useState)(false), 2), internalLoading = _useState[0], setInternalLoading = _useState[1];
    var loading = propLoading !== undefined ? propLoading : internalLoading;
    var setLoading = function setLoading(val) {
        if (propLoading !== undefined) {
            onLoadingChange(val);
        } else {
            setInternalLoading(val);
        }
    };
    (0, _react.useEffect)(function() {
        loading && setLoading(false);
    }, [
        value
    ]);
    var classes = function classes() {
        var _obj;
        return (0, _classnames.default)([
            classPrefix,
            className,
            (_obj = {}, (0, _define_property._)(_obj, "".concat(classPrefix, "-close"), !value), (0, _define_property._)(_obj, "".concat(classPrefix, "-disabled"), disabled), (0, _define_property._)(_obj, "".concat(classPrefix, "-disabled-close"), disabled && !value), _obj)
        ]);
    };
    var onClick = function onClick() {
        return (0, _async_to_generator._)(function() {
            var e;
            return (0, _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.default.createElement(_components.View, (0, _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.default.createElement(_components.View, {
        className: (0, _classnames.default)([
            [
                "".concat(classPrefix, "-button")
            ],
            [
                value ? "".concat(classPrefix, "-button-open") : "".concat(classPrefix, "-button-close")
            ],
            (_obj = {}, (0, _define_property._)(_obj, "".concat(classPrefix, "-button-open-rtl"), rtl && value), (0, _define_property._)(_obj, "".concat(classPrefix, "-button-close-rtl"), rtl && !value), _obj)
        ])
    }, loading && loadingIcon ? /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, loadingIcon) : /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, !value && !activeText && /*#__PURE__*/ _react.default.createElement(_components.View, {
        className: "".concat(classPrefix, "-close-line")
    }))), activeText && /*#__PURE__*/ _react.default.createElement(_components.View, {
        className: (0, _classnames.default)("".concat(classPrefix, "-label"), (_obj1 = {}, (0, _define_property._)(_obj1, "".concat(classPrefix, "-label-open"), value), (0, _define_property._)(_obj1, "".concat(classPrefix, "-label-close"), !value), (0, _define_property._)(_obj1, "".concat(classPrefix, "-label-open-rtl"), rtl && value), (0, _define_property._)(_obj1, "".concat(classPrefix, "-label-close-rtl"), rtl && !value), (0, _define_property._)(_obj1, "".concat(classPrefix, "-label-close-disabled"), disabled && !value), _obj1))
    }, value ? activeText : inactiveText));
};
Switch.displayName = 'NutSwitch';
