"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "Segmented", {
    enumerable: true,
    get: function() {
        return Segmented;
    }
});
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 _sliced_to_array = require("@swc/helpers/_/_sliced_to_array");
var _type_of = require("@swc/helpers/_/_type_of");
var _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
var _classnames = /*#__PURE__*/ _interop_require_default._(require("classnames"));
var _components = require("@tarojs/components");
var _usepropsvalue = require("../../hooks/use-props-value");
var _mergeprops = require("../../utils/merge-props");
var defaultProps = {
    options: [],
    onChange: function onChange(value) {}
};
var Segmented = function Segmented(props) {
    var classPrefix = 'nut-segmented';
    var itemClassPrefix = 'nut-segmented-item';
    var mergedProps = (0, _mergeprops.mergeProps)(defaultProps, props);
    var options = mergedProps.options, onChange = mergedProps.onChange;
    var _usePropsValue = (0, _sliced_to_array._)((0, _usepropsvalue.usePropsValue)({
        value: mergedProps.value,
        defaultValue: mergedProps.defaultValue,
        finalValue: 0,
        onChange: mergedProps.onChange
    }), 2), value = _usePropsValue[0], setValue = _usePropsValue[1];
    var renderItems = (0, _react.useCallback)(function(options, value) {
        return options.map(function(option, index) {
            var optionType = typeof option === "undefined" ? "undefined" : (0, _type_of._)(option);
            switch(optionType){
                case 'object':
                    // eslint-disable-next-line no-case-declarations
                    var opt = option;
                    var _obj;
                    return /*#__PURE__*/ _react.default.createElement(_components.View, {
                        className: (0, _classnames.default)(itemClassPrefix, (_obj = {}, (0, _define_property._)(_obj, "".concat(itemClassPrefix, "-active"), opt.value === value), (0, _define_property._)(_obj, "".concat(opt.className), !!opt.className), _obj)),
                        key: opt.value,
                        onClick: function onClick() {
                            if (opt.disabled) return;
                            setValue(option.value);
                        }
                    }, opt.icon ? /*#__PURE__*/ _react.default.createElement(_components.View, {
                        className: "nut-segmented-icon"
                    }, opt.icon) : null, opt.label);
                default:
                    {
                        if (typeof option !== 'string' && typeof option !== 'number') {
                            console.warn('Unsupported option type:', optionType);
                        }
                        return /*#__PURE__*/ _react.default.createElement(_components.View, {
                            className: (0, _classnames.default)(itemClassPrefix, (0, _define_property._)({}, "".concat(itemClassPrefix, "-active"), index === value)),
                            key: option.toString(),
                            onClick: function onClick() {
                                setValue(index);
                            }
                        }, option);
                    }
            }
        });
    }, [
        value
    ]);
    return /*#__PURE__*/ _react.default.createElement(_components.View, {
        className: (0, _classnames.default)(classPrefix),
        style: mergedProps.style
    }, renderItems(options, value));
};
