"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useAutopopulationContextValue = void 0;
var react_1 = require("react");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var MdmModuleContext_1 = require("../contexts/MdmModuleContext");
var useAutopopulationContextValue = function (_a) {
    var enabled = _a.enabled, attributeTypeUri = _a.attributeTypeUri, valueUri = _a.valueUri;
    var dependentLookupsEditorTouched = (0, MdmModuleContext_1.useMdmAction)('dependentLookupsEditorTouched');
    var autocompleteConfig = (0, MdmModuleContext_1.useMdmLookupAutocomplete)();
    var dependentLookupEditorState = (0, MdmModuleContext_1.useMdmDependentLookupEditorState)(valueUri);
    var autopopulationEnabled = enabled && (0, mdm_sdk_1.isAutopopulationEnabled)(autocompleteConfig, attributeTypeUri);
    var autopopulationId = autopopulationEnabled ? valueUri : null;
    var _b = ((autopopulationEnabled && dependentLookupEditorState) || {}).isTouched, isTouched = _b === void 0 ? false : _b;
    var onTouch = (0, react_1.useCallback)(function (valueUri) {
        dependentLookupsEditorTouched({ uri: valueUri, isTouched: true });
    }, [dependentLookupsEditorTouched]);
    var autopopulationContextValue = (0, react_1.useMemo)(function () { return (autopopulationId ? { id: autopopulationId, isTouched: isTouched, onTouch: onTouch } : null); }, [autopopulationId, isTouched, onTouch]);
    return { autopopulationContextValue: autopopulationContextValue };
};
exports.useAutopopulationContextValue = useAutopopulationContextValue;
