"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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CheckboxWithoutUnform = exports.CheckboxWithUnform = exports.SwitchCore = void 0;
var react_1 = __importStar(require("react"));
var core_1 = require("@unform/core");
var _ = __importStar(require("lodash"));
exports.SwitchCore = react_1.default.forwardRef(function Checkbox(props, inputRef) {
    if (props.name) {
        return react_1.default.createElement(exports.CheckboxWithUnform, __assign({}, props, { ref: inputRef }));
    }
    return react_1.default.createElement(exports.CheckboxWithoutUnform, __assign({}, props, { ref: inputRef }));
});
exports.CheckboxWithUnform = react_1.default.forwardRef(function CheckboxWithUnform(props, inputRef) {
    var _a = props.valueTrue, valueTrue = _a === void 0 ? true : _a, _b = props.valueFalse, valueFalse = _b === void 0 ? false : _b;
    var _c = core_1.useField(props.name), fieldName = _c.fieldName, registerField = _c.registerField, _d = _c.defaultValue, defaultValue = _d === void 0 ? null : _d, error = _c.error, clearError = _c.clearError;
    var inputValueRef = react_1.useRef(defaultValue);
    var _e = react_1.useState(_.isEqual(defaultValue, valueTrue)), checked = _e[0], setChecked = _e[1];
    react_1.useEffect(function () {
        registerField({
            name: fieldName,
            ref: inputValueRef,
            path: 'current',
            clearValue: function (ref) {
                ref.current = valueFalse;
                setChecked(false);
            },
            setValue: function (ref, v) {
                ref.current = v;
                setChecked(_.isEqual(v, valueTrue));
            },
            getValue: function (ref) {
                return ref.current;
            },
        });
    }, [fieldName, registerField]);
    var onChange = react_1.useCallback(function onSelectItem(newValue) {
        if (error)
            clearError();
        setChecked(!!newValue);
        inputValueRef.current = !!newValue;
        if (props.onChange)
            props.onChange(newValue);
    }, [error]);
    return react_1.default.createElement(exports.CheckboxWithoutUnform, __assign({}, props, { ref: inputRef, onChange: onChange, checked: checked, error: error }));
});
exports.CheckboxWithoutUnform = react_1.default.forwardRef(function CheckboxWithoutUnform(props, inputRef) {
    var ref = react_1.useRef(null);
    react_1.useImperativeHandle(inputRef, function () { return ref.current; }, []);
    var PureComponent = react_1.useMemo(function () { return props.as; }, [props.as]);
    return react_1.default.createElement(PureComponent, __assign({}, props, { ref: ref }));
});
//# sourceMappingURL=SwitchCore.js.map