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