"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.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.Switch = void 0;
var react_1 = __importStar(require("react"));
var core_1 = require("@unform/core");
var _ = __importStar(require("lodash"));
var Switch_style_1 = __importDefault(require("./Switch.style"));
exports.Switch = function (_a) {
    var _b;
    var children = _a.children, name = _a.name, _c = _a.value, value = _c === void 0 ? true : _c, 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 _d = core_1.useField(name), fieldName = _d.fieldName, registerField = _d.registerField, defaultValue = _d.defaultValue, error = _d.error, clearError = _d.clearError;
    var defaultChecked = _.isEqual(defaultValue, value);
    var _e = react_1.useState(defaultChecked), checked = _e[0], setChecked = _e[1];
    react_1.useEffect(function () {
        registerField({
            name: fieldName,
            ref: inputRef.current,
            path: 'value',
            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]);
    return (react_1.default.createElement(Switch_style_1.default, { color: color, checked: checked },
        react_1.default.createElement("div", { className: "checkbox-container" },
            react_1.default.createElement("label", { "data-cy": dataCy },
                react_1.default.createElement("div", { className: "switch-btn-container " + (rest.disabled && 'disabled') },
                    react_1.default.createElement("input", __assign({ type: "checkbox", ref: inputRef, defaultChecked: defaultChecked }, rest, { onChange: function (ev) {
                            setChecked(ev.target.checked);
                            if (error)
                                clearError();
                        } })),
                    react_1.default.createElement("span", { className: "switch-button " + (error && 'has-error') + " " + (rest.disabled && 'disabled'), "data-testid": "input-switch" })),
                react_1.default.createElement("span", { className: "title " + (rest.disabled && 'disabled') }, title))),
        error && (react_1.default.createElement("div", { "data-cy": "input-error." + ((_b = rest['data-cy']) !== null && _b !== void 0 ? _b : ''), className: "error-input", "data-testid": "input-error" }, error))));
};
//# sourceMappingURL=Switch.js.map