"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useAutopopulation = void 0;
var react_1 = require("react");
var DependentLookupAutopopulationContext_1 = require("../contexts/DependentLookupAutopopulationContext");
// Autopopulation for just added editor. Other autopopulation cases should be covered by cascadeUpdate flow.
var useAutopopulation = function (_a) {
    var initialDefaultOptions = _a.initialDefaultOptions, isEmptyValue = _a.isEmptyValue, multiple = _a.multiple, onChange = _a.onChange;
    var _b = (0, react_1.useContext)(DependentLookupAutopopulationContext_1.DependentLookupAutopopulationContext) || {}, id = _b.id, isTouched = _b.isTouched, onTouch = _b.onTouch;
    (0, react_1.useEffect)(function () {
        if (id && initialDefaultOptions && initialDefaultOptions.length === 1 && isEmptyValue && !isTouched) {
            onChange(multiple ? initialDefaultOptions : initialDefaultOptions[0]);
        }
        //eslint-disable-next-line react-hooks/exhaustive-deps
    }, [initialDefaultOptions]);
    var markAsTouched = (0, react_1.useCallback)(function () {
        if (id) {
            onTouch === null || onTouch === void 0 ? void 0 : onTouch(id);
        }
    }, [onTouch, id]);
    return { markAsTouched: markAsTouched };
};
exports.useAutopopulation = useAutopopulation;
