"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 __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SelectAutocomplete = void 0;
var react_1 = __importStar(require("react"));
var core_1 = require("@unform/core");
var Autocomplete_1 = require("./Autocomplete");
var SelectAutocomplete_style_1 = __importDefault(require("./SelectAutocomplete.style"));
function getData(data) {
    if (!data) {
        return null;
    }
    if (typeof data === 'string') {
        return data;
    }
    return data;
}
function getDataLabel(data) {
    if (!data) {
        return '';
    }
    if (typeof data === 'string') {
        return data;
    }
    return data.label;
}
function SelectAutocomplete(_a) {
    var name = _a.name, fetch = _a.fetch, title = _a.title, prefix = _a.prefix, getValue = _a.getValue, placeholder = _a.placeholder, _b = _a.clearable, clearable = _b === void 0 ? true : _b, renderInput = _a.renderInput;
    var autocompleteRef = react_1.useRef();
    var _c = core_1.useField(name), registerField = _c.registerField, _d = _c.defaultValue, defaultValue = _d === void 0 ? '' : _d, error = _c.error, fieldName = _c.fieldName;
    var valueRef = react_1.useRef(getData(defaultValue));
    var _e = react_1.useState(getDataLabel(defaultValue)), value = _e[0], setValue = _e[1];
    react_1.useEffect(function () {
        registerField({
            name: fieldName,
            ref: valueRef,
            path: 'current',
            setValue: function (ref, _value) {
                ref = getData(_value);
            },
            getValue: function (ref) {
                return valueRef.current;
            },
        });
    }, [fieldName, registerField]);
    function onSelect(i) {
        valueRef.current = getValue ? getValue(i) : i;
        setValue(getDataLabel(valueRef.current));
    }
    return (react_1.default.createElement(SelectAutocomplete_style_1.default, { "data-cy": "autocomplete-" + name },
        react_1.default.createElement("div", { className: "select-box-autocomplete" },
            react_1.default.createElement(Autocomplete_1.Autocomplete, { ref: autocompleteRef, name: name, title: title, fetch: fetch, valueSelected: value, error: !!error, prefix: prefix, placeholder: placeholder, clearable: clearable, cleanAfterSelect: true, renderInput: renderInput, select: onSelect })),
        !!error && (react_1.default.createElement("div", { className: "error-input", "data-testid": "input-error" }, error))));
}
exports.SelectAutocomplete = SelectAutocomplete;
//# sourceMappingURL=SelectAutocomplete.js.map