import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
/**
 * Copyright (c) 2020-present, Goldman Sachs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { useCallback, useRef } from 'react';
import { observer } from 'mobx-react-lite';
import { EmbeddedRelationalInstanceSetImplementationState, } from '../../../../../stores/editor/editor-state/element-editor-state/mapping/relational/RelationalInstanceSetImplementationState.js';
import { MappingEditorState, } from '../../../../../stores/editor/editor-state/element-editor-state/mapping/MappingEditorState.js';
import { clsx, CustomSelectorInput, ArrowCircleRightIcon, } from '@finos/legend-art';
import { useDrop } from 'react-dnd';
import { guaranteeType } from '@finos/legend-shared';
import { TableOrViewTreeNodeDragSource, TABLE_ELEMENT_DND_TYPE, } from './TableOrViewSourceTree.js';
import { useEditorStore } from '../../../EditorStoreProvider.js';
import { Enumeration, EnumerationMapping, RelationalPropertyMapping, getEnumerationMappingsByEnumeration, getRawGenericType, EnumerationMappingExplicitReference, } from '@finos/legend-graph';
import { relationalPropertyMapping_setTransformer } from '../../../../../stores/graph-modifier/STO_Relational_GraphModifierHelper.js';
import { getExpectedReturnType } from '../PropertyMappingsEditor.js';
import { CLASS_PROPERTY_TYPE, getClassPropertyType, } from '../../../../../stores/editor/utils/ModelClassifierUtils.js';
import { InlineLambdaEditor } from '@finos/legend-query-builder';
const SimplePropertyMappingEditor = observer((props) => {
    const { propertyMappingState, dropConnector, transformProps } = props;
    const ref = useRef(null);
    dropConnector?.(ref);
    return (_jsx("div", { className: "property-mapping-editor__entry__container", children: _jsx("div", { ref: ref, className: "property-mapping-editor__entry", children: _jsx(InlineLambdaEditor, { disabled: transformProps.disableTransform, lambdaEditorState: propertyMappingState, forceBackdrop: transformProps.forceBackdrop }) }) }));
});
const EnumerationPropertyMappingEditor = observer((props) => {
    const { propertyMappingState, dropConnector, transformProps, isReadOnly } = props;
    const ref = useRef(null);
    dropConnector?.(ref);
    const editorStore = useEditorStore();
    const mappingEditorState = editorStore.tabManagerState.getCurrentEditorState(MappingEditorState);
    const propertyMapping = guaranteeType(propertyMappingState.propertyMapping, RelationalPropertyMapping, 'Relational property mapping for enumeration type property must be a simple property mapping');
    const enumeration = getRawGenericType(propertyMapping.property.value.genericType.value, Enumeration);
    // Enumeration Mapping Selector
    const options = getEnumerationMappingsByEnumeration(mappingEditorState.mapping, enumeration).map((em) => ({ value: em, label: em.id.value }));
    const handleSelectionChange = (val) => relationalPropertyMapping_setTransformer(propertyMapping, val?.value
        ? EnumerationMappingExplicitReference.create(val.value)
        : undefined);
    // Walker
    const visit = () => {
        const currentTransformerEnumerationMapping = propertyMapping.transformer?.value;
        if (currentTransformerEnumerationMapping) {
            mappingEditorState.openMappingElement(currentTransformerEnumerationMapping, true);
        }
        else {
            if (!isReadOnly) {
                mappingEditorState.createMappingElement({
                    target: enumeration,
                    showTarget: false,
                    openInAdjacentTab: true,
                    postSubmitAction: (newEnumerationMapping) => {
                        if (newEnumerationMapping instanceof EnumerationMapping) {
                            relationalPropertyMapping_setTransformer(propertyMapping, EnumerationMappingExplicitReference.create(newEnumerationMapping));
                        }
                    },
                });
            }
        }
    };
    return (_jsx("div", { className: "property-mapping-editor__entry__container", children: _jsxs("div", { ref: ref, className: "property-mapping-editor__entry", children: [_jsxs("div", { className: "property-mapping-editor__entry__enumeration-mapping-selector", children: [_jsx(CustomSelectorInput, { disabled: options.length <= 1 || isReadOnly, options: options, onChange: handleSelectionChange, value: propertyMapping.transformer
                                ? {
                                    value: propertyMapping.transformer.value,
                                    label: propertyMapping.transformer.valueForSerialization ?? '',
                                }
                                : null, placeholder: "Choose an existing enumeration mapping" }), _jsx("button", { className: "property-mapping-editor__entry__visit-btn", onClick: visit, tabIndex: -1, title: "Visit enumeration mapping", children: _jsx(ArrowCircleRightIcon, {}) })] }), _jsx(InlineLambdaEditor, { className: clsx('property-mapping-editor__entry__enumeration__transform'), disabled: transformProps.disableTransform, lambdaEditorState: propertyMappingState, forceBackdrop: transformProps.forceBackdrop })] }) }));
});
const ClassPropertyMappingEditor = observer((props) => {
    const { propertyMappingState, dropConnector, transformProps } = props;
    const ref = useRef(null);
    dropConnector?.(ref);
    const editorStore = useEditorStore();
    const mappingEditorState = editorStore.tabManagerState.getCurrentEditorState(MappingEditorState);
    const propertyMapping = propertyMappingState.propertyMapping;
    const isDefaultId = propertyMapping.targetSetImplementation?.value.id.isDefault;
    const target = isDefaultId ? (_jsx("div", { className: "property-mapping-editor__entry__id__label__default-badge", children: "default" })) : (propertyMapping.targetSetImplementation?.value.id.value);
    const expectedType = getExpectedReturnType(propertyMapping.targetSetImplementation?.value);
    const onExpectedTypeLabelSelect = () => propertyMappingState.instanceSetImplementationState.setSelectedType(expectedType);
    const matchedExpectedTypeLabel = () => Boolean(expectedType) &&
        propertyMappingState.instanceSetImplementationState.selectedType ===
            expectedType;
    // Walker
    const visit = () => {
        if (propertyMapping.targetSetImplementation?.value) {
            mappingEditorState.openMappingElement(propertyMapping.targetSetImplementation.value, true);
        }
    };
    return (_jsx("div", { className: "property-mapping-editor__entry__container", children: _jsxs("div", { ref: ref, className: "property-mapping-editor__entry", children: [_jsxs("div", { className: "property-mapping-editor__entry__id", children: [_jsx("div", { className: clsx('property-mapping-editor__entry__id__label', {
                                'property-mapping-editor__entry__id__label--default': isDefaultId,
                            }), children: target }), _jsx("button", { className: "property-mapping-editor__entry__visit-btn", onClick: visit, tabIndex: -1, title: "Visit class mapping", children: _jsx(ArrowCircleRightIcon, {}) })] }), _jsx(InlineLambdaEditor, { disabled: transformProps.disableTransform, lambdaEditorState: propertyMappingState, forceBackdrop: transformProps.forceBackdrop, expectedType: expectedType, onExpectedTypeLabelSelect: onExpectedTypeLabelSelect, matchedExpectedType: matchedExpectedTypeLabel })] }) }));
});
export const RelationalPropertyMappingEditor = observer((props) => {
    const { relationalPropertyMappingState, relationalInstanceSetImplementationState, setImplementationHasParserError, isReadOnly, } = props;
    const disableEditingTransform = relationalInstanceSetImplementationState.isConvertingTransformLambdaObjects ||
        isReadOnly;
    // Drag and Drop
    const handleDrop = useCallback((droppedItem) => {
        if (!disableEditingTransform) {
            if (droppedItem instanceof TableOrViewTreeNodeDragSource) {
                const toAppend = droppedItem.data.id;
                if (toAppend) {
                    relationalPropertyMappingState.setLambdaString(relationalPropertyMappingState.lambdaString + toAppend);
                }
            }
        }
    }, [disableEditingTransform, relationalPropertyMappingState]);
    const [, dropConnector] = useDrop(() => ({
        accept: [TABLE_ELEMENT_DND_TYPE],
        drop: (item) => handleDrop(item),
    }), [handleDrop]);
    const transformProps = {
        disableTransform: relationalInstanceSetImplementationState.isConvertingTransformLambdaObjects,
        forceBackdrop: setImplementationHasParserError,
    };
    switch (getClassPropertyType(relationalPropertyMappingState.propertyMapping.property.value
        .genericType.value.rawType)) {
        case CLASS_PROPERTY_TYPE.UNIT:
        case CLASS_PROPERTY_TYPE.MEASURE:
        case CLASS_PROPERTY_TYPE.PRIMITIVE:
            return (_jsx(SimplePropertyMappingEditor, { propertyMappingState: relationalPropertyMappingState, dropConnector: dropConnector, transformProps: transformProps, isReadOnly: isReadOnly }));
        case CLASS_PROPERTY_TYPE.ENUMERATION:
            return (_jsx(EnumerationPropertyMappingEditor, { propertyMappingState: relationalPropertyMappingState, dropConnector: dropConnector, transformProps: transformProps, isReadOnly: isReadOnly }));
        case CLASS_PROPERTY_TYPE.CLASS: {
            if (relationalPropertyMappingState instanceof
                EmbeddedRelationalInstanceSetImplementationState) {
                return (_jsx("div", { className: "property-mapping-editor__entry--embedded", children: "Embedded property mapping specified, but not supported in form mode" }));
            }
            return (_jsx(ClassPropertyMappingEditor, { propertyMappingState: relationalPropertyMappingState, dropConnector: dropConnector, transformProps: transformProps, isReadOnly: isReadOnly }));
        }
        default:
            return null;
    }
});
//# sourceMappingURL=RelationalPropertyMappingEditor.js.map