"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
    for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
        to[j] = from[i];
    return to;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ButtonsGroup = void 0;
var react_1 = __importStar(require("react"));
var core_1 = require("@unform/core");
var styles_1 = require("../styles");
var ButtonGroup_interface_1 = require("./ButtonGroup.interface");
var ButtonGroup_style_1 = __importStar(require("./ButtonGroup.style"));
var ButtonsGroup = function (props) {
    if (props.name) {
        return react_1.default.createElement(ButtonsGroupWithUnform, __assign({}, props));
    }
    return react_1.default.createElement(ButtonsGroupWithoutUnform, __assign({}, props));
};
exports.ButtonsGroup = ButtonsGroup;
var ButtonsGroupWithUnform = function (props) {
    var _a = core_1.useField(props.name), fieldName = _a.fieldName, registerField = _a.registerField, _b = _a.defaultValue, defaultValue = _b === void 0 ? null : _b, error = _a.error, clearError = _a.clearError;
    var inputRef = react_1.useRef(ButtonGroup_interface_1.getValue(defaultValue));
    var _c = react_1.useState(ButtonGroup_interface_1.getValue(defaultValue)), value = _c[0], setValue = _c[1];
    react_1.useEffect(function () {
        registerField({
            name: fieldName,
            ref: inputRef,
            path: 'current',
            clearValue: function (ref) {
                ref.current = null;
                setValue(ref.current);
            },
            setValue: function (ref, v) {
                ref.current = v || null;
                if (props.multiSelection && !(v instanceof Array)) {
                    ref.current = [];
                }
                setValue(ref.current);
            },
            getValue: function (ref) {
                return ref.current;
            },
        });
    }, [fieldName, registerField]);
    var onSelectItem = react_1.useCallback(function onSelectItem(newValue, currentOption) {
        if (error)
            clearError();
        setValue(newValue);
        inputRef.current = newValue;
        if (props.onSelect)
            props.onSelect(newValue, currentOption);
    }, [error]);
    return (react_1.default.createElement(ButtonsGroupWithoutUnform, __assign({}, props, { onSelect: onSelectItem, defaultVale: defaultValue, value: value, error: error })));
};
var ButtonsGroupWithoutUnform = function (_a) {
    var options = _a.options, name = _a.name, value = _a.value, title = _a.title, variant = _a.variant, error = _a.error, _b = _a.multiSelection, multiSelection = _b === void 0 ? false : _b, _c = _a.allowEmpty, allowEmpty = _c === void 0 ? false : _c, onSelect = _a.onSelect, activeBtnStyle = _a.activeBtnStyle, activeTextStyle = _a.activeTextStyle, rounded = _a.rounded, separate = _a.separate, fullLine = _a.fullLine, outline = _a.outline, oneLine = _a.oneLine, inactiveBtnStyle = _a.inactiveBtnStyle, inactiveTextStyle = _a.inactiveTextStyle, rest = __rest(_a, ["options", "name", "value", "title", "variant", "error", "multiSelection", "allowEmpty", "onSelect", "activeBtnStyle", "activeTextStyle", "rounded", "separate", "fullLine", "outline", "oneLine", "inactiveBtnStyle", "inactiveTextStyle"]);
    var select = react_1.useCallback(function (option) {
        var newValue;
        if (multiSelection) {
            var currentValue = !Array.isArray(value) ? [] : __spreadArray([], value);
            if (currentValue.indexOf(option.value) === -1) {
                currentValue.push(option.value);
            }
            else {
                currentValue.splice(currentValue.indexOf(option.value), 1);
            }
            newValue = currentValue;
        }
        else if (allowEmpty) {
            newValue = value === option.value ? null : option.value;
        }
        else {
            newValue = option.value;
        }
        if (onSelect)
            onSelect(newValue, option);
    }, [value, onSelect]);
    return (react_1.default.createElement(styles_1.FormStyle, null,
        react_1.default.createElement(ButtonGroup_style_1.default, { fullLine: fullLine, oneLine: oneLine },
            title && react_1.default.createElement("div", { className: "input-title" }, title),
            react_1.default.createElement("div", { className: "btn-group btn-selects " + (!!error && 'has-error') }, options &&
                options.map(function (option, index) { return (react_1.default.createElement(ButtonSingle, { index: index, btnLength: options.length, select: select, rounded: rounded, option: option, fullLine: fullLine, separate: separate, key: option.value, variant: variant, outline: outline, activeBtnStyle: activeBtnStyle, activeTextStyle: activeTextStyle, inactiveBtnStyle: inactiveBtnStyle, inactiveTextStyle: inactiveTextStyle, active: multiSelection
                        ? Array.isArray(value) && value.indexOf(option.value) !== -1
                        : value === option.value })); })),
            error && (react_1.default.createElement("div", { className: "error-input", "data-testid": "input-error}" }, error)))));
};
var ButtonSingle = react_1.default.memo(function ButtonSingle(_a) {
    var option = _a.option, select = _a.select, active = _a.active, variant = _a.variant, index = _a.index, btnLength = _a.btnLength, rounded = _a.rounded, separate = _a.separate, fullLine = _a.fullLine, outline = _a.outline;
    return (react_1.default.createElement(ButtonGroup_style_1.ButtonStyle, { className: "btn " + (active && 'active'), onClick: function () { return select(option); }, variant: variant, active: !!active, startBtn: index == 0, startRounded: index == 0 && rounded, endRounded: btnLength - 1 == index && rounded, endBtn: btnLength - 1 == index, outline: outline, rounded: separate && rounded, separate: separate, "data-testid": "button-" + option.name, "data-active": !!active }, option.name));
});
//# sourceMappingURL=ButtonGroup.js.map