import { useCallback, useContext, useEffect } from 'react';
import { DependentLookupAutopopulationContext } from '../contexts/DependentLookupAutopopulationContext';
// Autopopulation for just added editor. Other autopopulation cases should be covered by cascadeUpdate flow.
export var useAutopopulation = function (_a) {
    var initialDefaultOptions = _a.initialDefaultOptions, isEmptyValue = _a.isEmptyValue, multiple = _a.multiple, onChange = _a.onChange;
    var _b = useContext(DependentLookupAutopopulationContext) || {}, id = _b.id, isTouched = _b.isTouched, onTouch = _b.onTouch;
    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 = useCallback(function () {
        if (id) {
            onTouch === null || onTouch === void 0 ? void 0 : onTouch(id);
        }
    }, [onTouch, id]);
    return { markAsTouched: markAsTouched };
};
