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 React, { useContext, useEffect, useState } from "react";
import { CheckDisabled, Checked, CheckNormal } from "@nutui/icons-react-taro";
import classNames from "classnames";
import { View } from "@tarojs/components";
import { ComponentDefaults } from "../../utils/typings";
import Context from "../checkboxgroup/context";
import { usePropsValue } from "../../hooks/use-props-value";
var defaultProps = _object_spread_props(_object_spread({}, ComponentDefaults), {
    disabled: false,
    shape: 'round',
    labelPosition: 'right',
    icon: null,
    activeIcon: null,
    indeterminateIcon: null,
    onChange: function(value) {}
});
var classPrefix = 'nut-checkbox';
export var Checkbox = function(props) {
    var children = _object_spread({}, defaultProps, props).children;
    var icon = props.icon, label = props.label, className = props.className, activeIcon = props.activeIcon, checked = props.checked, value = props.value, defaultChecked = props.defaultChecked, shape = props.shape, disabled = props.disabled, onChange = props.onChange, indeterminate = props.indeterminate, indeterminateIcon = props.indeterminateIcon, others = _object_without_properties(props, [
        "icon",
        "label",
        "className",
        "activeIcon",
        "checked",
        "value",
        "defaultChecked",
        "shape",
        "disabled",
        "onChange",
        "indeterminate",
        "indeterminateIcon"
    ]);
    // eslint-disable-next-line prefer-const
    var labelPosition = others.labelPosition, rest = _object_without_properties(others, [
        "labelPosition"
    ]);
    var ctx = useContext(Context);
    var _usePropsValue = _sliced_to_array(usePropsValue({
        value: checked,
        defaultValue: defaultChecked,
        finalValue: defaultChecked,
        onChange: onChange
    }), 2), innerChecked = _usePropsValue[0], setChecked = _usePropsValue[1];
    // eslint-disable-next-line prefer-const
    var _useState = _sliced_to_array(useState(disabled), 2), innerDisabled = _useState[0], setDisabled = _useState[1];
    var _useState1 = _sliced_to_array(useState(indeterminate), 2), innerIndeterminate = _useState1[0], setIndeterminate = _useState1[1];
    useEffect(function() {
        setDisabled(disabled);
    }, [
        disabled
    ]);
    useEffect(function() {
        setIndeterminate(indeterminate);
    }, [
        indeterminate
    ]);
    if (ctx) {
        if (ctx.labelPosition !== undefined) {
            labelPosition = ctx.labelPosition;
        }
        innerDisabled = ctx.disabled !== undefined ? ctx.disabled : innerDisabled;
        innerChecked = ctx.value.includes(value);
        setChecked = function(checked) {
            if (ctx.disabled) return;
            if (checked) ctx.check(value);
            if (!checked) ctx.uncheck(value);
        };
    }
    var renderIcon = function() {
        if (innerDisabled) {
            if (innerIndeterminate) {
                // @ts-ignore
                return /*#__PURE__*/ React.createElement(CheckDisabled, {
                    className: color(),
                    ariaHidden: true
                });
            }
            if (innerChecked) {
                // @ts-ignore
                return /*#__PURE__*/ React.createElement(Checked, {
                    className: color(),
                    ariaHidden: true
                });
            }
            // @ts-ignore
            return /*#__PURE__*/ React.createElement(CheckDisabled, {
                className: color(),
                ariaHidden: true
            });
        }
        if (!innerChecked) {
            return /*#__PURE__*/ React.isValidElement(icon) ? icon : // @ts-ignore
            /*#__PURE__*/ React.createElement(CheckNormal, {
                className: color(),
                ariaHidden: true
            });
        }
        if (innerIndeterminate) {
            return /*#__PURE__*/ React.isValidElement(indeterminateIcon) ? indeterminateIcon : // @ts-ignore
            /*#__PURE__*/ React.createElement(CheckDisabled, {
                className: color(),
                ariaHidden: true
            });
        }
        return /*#__PURE__*/ React.isValidElement(activeIcon) ? activeIcon : /*#__PURE__*/ React.createElement(View, {
            className: "".concat(classPrefix, "-icon-wrap")
        }, /*#__PURE__*/ React.createElement(Checked, {
            className: color(),
            ariaHidden: true
        }));
    };
    var color = function() {
        var cls = "".concat(classPrefix, "-icon ");
        if (innerDisabled) {
            if (innerChecked && !innerIndeterminate) {
                return "".concat(cls).concat(classPrefix, "-icon-checked ").concat(classPrefix, "-icon-disabled");
            }
            if (innerChecked && innerIndeterminate) {
                return "".concat(cls).concat(classPrefix, "-icon-indeterminate ").concat(classPrefix, "-icon-disabled");
            }
            return "".concat(cls).concat(classPrefix, "-icon-disabled");
        }
        if (innerChecked) {
            if (innerIndeterminate) {
                return "".concat(cls).concat(classPrefix, "-icon-indeterminate");
            }
            return "".concat(cls).concat(classPrefix, "-icon-checked");
        }
        return cls;
    };
    var renderLabel = function() {
        var hasLabel = children || label;
        return hasLabel ? /*#__PURE__*/ React.createElement(View, {
            className: classNames("".concat(classPrefix, "-label "), _define_property({}, "".concat(classPrefix, "-label-disabled"), innerDisabled))
        }, hasLabel) : null;
    };
    var handleClick = function() {
        // 禁用的时候直接返回
        if (disabled) return;
        // 先转换状态
        var latestChecked = !innerChecked;
        setChecked(latestChecked);
    };
    var renderButton = function() {
        var _obj;
        return /*#__PURE__*/ React.createElement(View, {
            className: classNames("".concat(classPrefix, "-button"), (_obj = {}, _define_property(_obj, "".concat(classPrefix, "-button-active"), innerChecked), _define_property(_obj, "".concat(classPrefix, "-button-disabled"), disabled), _obj))
        }, children || label, innerChecked && activeIcon ? /*#__PURE__*/ React.createElement(React.Fragment, null, /*#__PURE__*/ React.createElement(View, {
            className: classNames("".concat(classPrefix, "-button-icon"))
        }), activeIcon) : null);
    };
    var renderListItem = function() {
        return /*#__PURE__*/ React.createElement(React.Fragment, null, renderIcon(), renderLabel());
    };
    var renderCheckboxItem = function() {
        if (ctx === null || ctx === void 0 ? void 0 : ctx.list) {
            return /*#__PURE__*/ React.createElement(React.Fragment, null, renderListItem());
        }
        if (shape === 'button') {
            return renderButton();
        }
        return /*#__PURE__*/ React.createElement(React.Fragment, null, renderIcon(), renderLabel());
    };
    var _obj;
    return /*#__PURE__*/ React.createElement(View, _object_spread_props(_object_spread({
        className: classNames(classPrefix, (_obj = {}, _define_property(_obj, "".concat(classPrefix, "-reverse"), labelPosition === 'left'), _define_property(_obj, 'nut-checkbox-list-item', ctx === null || ctx === void 0 ? void 0 : ctx.list), _define_property(_obj, "".concat(classPrefix, "-active"), innerChecked), _define_property(_obj, "".concat(classPrefix, "-nolabel"), !(children || label)), _obj), className)
    }, rest), {
        onClick: handleClick,
        ariaRole: "checkbox",
        tabIndex: innerDisabled ? -1 : 0,
        ariaChecked: innerIndeterminate ? 'mixed' : innerChecked
    }), renderCheckboxItem());
};
Checkbox.displayName = 'NutCheckBox';
