"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 __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Checkbox = void 0;
var react_1 = __importStar(require("react"));
var react_native_1 = require("react-native");
var checkbox_1 = __importDefault(require("@react-native-community/checkbox"));
var core_1 = require("@unform/core");
var _ = __importStar(require("lodash"));
var styled_components_1 = require("styled-components");
var Input_style_1 = require("../../Input/native/Input.style");
var Checkbox_style_1 = require("./Checkbox.style");
exports.Checkbox = function (_a) {
    var children = _a.children, name = _a.name, _b = _a.value, value = _b === void 0 ? true : _b, title = _a.title, color = _a.color, dataCy = _a["data-cy"], rest = __rest(_a, ["children", "name", "value", "title", "color", 'data-cy']);
    var inputRef = react_1.useRef(null);
    var _c = core_1.useField(name), fieldName = _c.fieldName, registerField = _c.registerField, defaultValue = _c.defaultValue, error = _c.error, clearError = _c.clearError;
    var defaultChecked = _.isEqual(defaultValue, value);
    var _d = react_1.useState(defaultChecked), isSelected = _d[0], setSelection = _d[1];
    var theme = styled_components_1.useTheme();
    react_1.useEffect(function () {
        registerField({
            name: fieldName,
            ref: inputRef,
            path: 'current',
            setValue: function (ref, _value) {
                ref.checked = _.isEqual(_value, value);
            },
            getValue: function (ref) {
                return ref.checked ? value : null;
            },
            clearValue: function (ref) {
                ref.checked = false;
            },
        });
    }, [fieldName, registerField, value]);
    var setCheckBox = function (v) {
        setSelection(v);
        inputRef.current = v;
    };
    return (react_1.default.createElement(react_native_1.View, null,
        react_1.default.createElement(Checkbox_style_1.CheckboxStyle, { onPress: function () { return setCheckBox(!isSelected); } },
            react_1.default.createElement(checkbox_1.default, { value: isSelected, onValueChange: setCheckBox, lineWidtH: 8, tintColors: { true: (theme === null || theme === void 0 ? void 0 : theme.primary) || '#222', false: '#a5a5a5' } }),
            title && react_1.default.createElement(Checkbox_style_1.TitleText, null, title)),
        !!error && react_1.default.createElement(Input_style_1.TextError, { testID: "checkbox-error" }, error)));
};
//# sourceMappingURL=Checkbox.js.map