import React, { useCallback, useMemo } from 'react';
import { createContext } from '@fluentui/react-context-selector';
import { getEntityType } from '@reltio/mdm-sdk';
import { useMdmAction, useMdmEntityUri, useMdmMetadata, useMdmModifiedEntity } from '../MdmModuleContext';
import { getSettings, isTriggerAttribute, modifyAttributes } from './helpers';
export var AddressAutoCompleteContext = createContext(null);
export var AddressAutoCompleteProvider = function (_a) {
    var children = _a.children, entityUri = _a.entityUri;
    var metadata = useMdmMetadata();
    var modifyAttribute = useMdmAction('modifyAttribute');
    var mdmEntityUri = useMdmEntityUri();
    var modifiedEntity = useMdmModifiedEntity(entityUri || mdmEntityUri) || {};
    var addressAutoCompleteConfig = useMemo(function () {
        var _a;
        var entityType = (modifiedEntity === null || modifiedEntity === void 0 ? void 0 : modifiedEntity.type) && metadata && getEntityType(metadata, modifiedEntity.type);
        return (((_a = entityType === null || entityType === void 0 ? void 0 : entityType.cleanseConfig) === null || _a === void 0 ? void 0 : _a.addressAutoCompleteConfig) ||
            { outputMapping: [], inputMapping: [] });
    }, [modifiedEntity === null || modifiedEntity === void 0 ? void 0 : modifiedEntity.type, metadata]);
    var onPopulateAttributes = useCallback(function (triggerAttributeTypeUri, triggerAttributeValueUri, values) {
        if (!(addressAutoCompleteConfig === null || addressAutoCompleteConfig === void 0 ? void 0 : addressAutoCompleteConfig.outputMapping) ||
            !(modifiedEntity === null || modifiedEntity === void 0 ? void 0 : modifiedEntity.uri) ||
            !triggerAttributeTypeUri ||
            !values) {
            return;
        }
        var outputMapping = addressAutoCompleteConfig.outputMapping;
        modifyAttributes({
            outputMapping: outputMapping,
            modifiedEntity: modifiedEntity,
            metadata: metadata,
            triggerAttributeTypeUri: triggerAttributeTypeUri,
            triggerAttributeValueUri: triggerAttributeValueUri,
            values: values,
            modifyAttribute: modifyAttribute
        });
    }, [addressAutoCompleteConfig, modifiedEntity, metadata, modifyAttribute]);
    var isAutocompleteTriggerAttribute = useCallback(function (attributeType) {
        var _a = addressAutoCompleteConfig.inputMapping, inputMapping = _a === void 0 ? [] : _a;
        return isTriggerAttribute(attributeType, inputMapping);
    }, [addressAutoCompleteConfig]);
    var getAutocompleteSettings = useCallback(function (triggerAttributeTypeUri, triggerAttributeValueUri) {
        return getSettings({
            metadata: metadata,
            modifiedEntity: modifiedEntity,
            triggerAttributeValueUri: triggerAttributeValueUri,
            triggerAttributeTypeUri: triggerAttributeTypeUri,
            addressAutoCompleteConfig: addressAutoCompleteConfig
        });
    }, [addressAutoCompleteConfig, metadata, modifiedEntity]);
    var contextValue = useMemo(function () { return ({
        onPopulateAttributes: onPopulateAttributes,
        isAutocompleteTriggerAttribute: isAutocompleteTriggerAttribute,
        getAutocompleteSettings: getAutocompleteSettings
    }); }, [onPopulateAttributes, isAutocompleteTriggerAttribute, getAutocompleteSettings]);
    return React.createElement(AddressAutoCompleteContext.Provider, { value: contextValue }, children);
};
