import { useCallback, useMemo } from 'react';
import { isAutopopulationEnabled } from '@reltio/mdm-sdk';
import { useMdmAction, useMdmDependentLookupEditorState, useMdmLookupAutocomplete } from '../contexts/MdmModuleContext';
export var useAutopopulationContextValue = function (_a) {
    var enabled = _a.enabled, attributeTypeUri = _a.attributeTypeUri, valueUri = _a.valueUri;
    var dependentLookupsEditorTouched = useMdmAction('dependentLookupsEditorTouched');
    var autocompleteConfig = useMdmLookupAutocomplete();
    var dependentLookupEditorState = useMdmDependentLookupEditorState(valueUri);
    var autopopulationEnabled = enabled && isAutopopulationEnabled(autocompleteConfig, attributeTypeUri);
    var autopopulationId = autopopulationEnabled ? valueUri : null;
    var _b = ((autopopulationEnabled && dependentLookupEditorState) || {}).isTouched, isTouched = _b === void 0 ? false : _b;
    var onTouch = useCallback(function (valueUri) {
        dependentLookupsEditorTouched({ uri: valueUri, isTouched: true });
    }, [dependentLookupsEditorTouched]);
    var autopopulationContextValue = useMemo(function () { return (autopopulationId ? { id: autopopulationId, isTouched: isTouched, onTouch: onTouch } : null); }, [autopopulationId, isTouched, onTouch]);
    return { autopopulationContextValue: autopopulationContextValue };
};
