"use strict";
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.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 __spreadArrays = (this && this.__spreadArrays) || function () {
    for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
    for (var r = Array(s), k = 0, i = 0; i < il; i++)
        for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
            r[k] = a[j];
    return r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ButtonsGroup = void 0;
var react_1 = __importStar(require("react"));
var react_native_1 = require("react-native");
var core_1 = require("@unform/core");
var ButtonGroup_interface_1 = require("../ButtonGroup.interface");
var ButtonGroup_style_1 = require("./ButtonGroup.style");
exports.ButtonsGroup = function (_a) {
    var options = _a.options, name = _a.name, title = _a.title, _b = _a.multiSelection, multiSelection = _b === void 0 ? false : _b, _c = _a.allowEmpty, allowEmpty = _c === void 0 ? false : _c, onChange = _a.onChange, rest = __rest(_a, ["options", "name", "title", "multiSelection", "allowEmpty", "onChange"]);
    var _d = core_1.useField(name), fieldName = _d.fieldName, registerField = _d.registerField, _e = _d.defaultValue, defaultValue = _e === void 0 ? '' : _e, error = _d.error, clearError = _d.clearError;
    var inputRef = react_1.useRef(ButtonGroup_interface_1.getValue(defaultValue));
    var _f = react_1.useState(ButtonGroup_interface_1.getValue(defaultValue)), value = _f[0], setValue = _f[1];
    react_1.useEffect(function () {
        registerField({
            name: fieldName,
            ref: inputRef,
            path: 'current',
            clearValue: function (ref) {
                if (multiSelection) {
                    ref.current = [];
                }
                else {
                    ref.current = '';
                }
                setValue(ref.current);
            },
            setValue: function (ref, v) {
                ref.current = v || '';
                if (multiSelection && !(v instanceof Array)) {
                    ref.current = [];
                }
                setValue(ref.current);
            },
            getValue: function (ref) {
                return ref.current;
            },
        });
    }, [fieldName, registerField, value]);
    var select = react_1.useCallback(function (_value) {
        clearError();
        if (multiSelection) {
            setValue(function (currentValue) {
                var newValue = __spreadArrays(currentValue);
                if (newValue.indexOf(_value) === -1) {
                    newValue.push(_value);
                }
                else {
                    newValue.splice(newValue.indexOf(_value), 1);
                }
                inputRef.current = newValue;
                return newValue;
            });
        }
        else if (allowEmpty) {
            inputRef.current = inputRef.current === _value ? null : _value;
            setValue(inputRef.current);
        }
        else {
            inputRef.current = _value;
            setValue(inputRef.current);
        }
        setTimeout(function () { return onChange && onChange(inputRef.current); }, 1);
    }, []);
    return (react_1.default.createElement(react_native_1.View, null,
        title && react_1.default.createElement(ButtonGroup_style_1.TitleText, null, title),
        react_1.default.createElement(ButtonGroup_style_1.ButtonsGroupStyle, { horizontal: true }, options &&
            options.map(function (option) { return (react_1.default.createElement(Button, { select: select, option: option, key: option.value, active: (multiSelection ? value.indexOf(option.value) !== -1 : value === option.value) && 'active' })); })),
        !!error && react_1.default.createElement(ButtonGroup_style_1.TextError, { testID: "input-error" }, error)));
};
var Button = react_1.default.memo(function Button(_a) {
    var option = _a.option, select = _a.select, active = _a.active;
    return (react_1.default.createElement(ButtonGroup_style_1.ButtonComponent, { onPress: function () { return select(option.value); }, key: option.value, active: active, testID: "button-" + option.value },
        react_1.default.createElement(ButtonGroup_style_1.ButtonText, { active: active }, option.name)));
});
//# sourceMappingURL=ButtonGroup.js.map