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 { observer } from 'mobx-react-lite';
import { InstanceSetImplementationEditor } from './InstanceSetImplementationEditor.js';
import { OperationSetImplementationEditor } from './OperationSetImplementationEditor.js';
import { clsx, CustomSelectorInput, PURE_ClassIcon, EmptySquareIcon, CheckSquareIcon, } from '@finos/legend-art';
import { getElementTypeIcon } from '../../../ElementIconUtils.js';
import { MappingEditorState } from '../../../../stores/editor/editor-state/element-editor-state/mapping/MappingEditorState.js';
import { useEditorStore } from '../../EditorStoreProvider.js';
import { PureInstanceSetImplementation, FlatDataInstanceSetImplementation, EmbeddedFlatDataPropertyMapping, RootRelationalInstanceSetImplementation, fromElementPathToMappingElementId, OperationSetImplementation, OperationType, INTERNAL__UnknownSetImplementation, RelationFunctionInstanceSetImplementation, } from '@finos/legend-graph';
import { setImpl_nominateRoot, operationMapping_setOperation, operationMapping_setParameters, setImplementation_setRoot, } from '../../../../stores/graph-modifier/DSL_Mapping_GraphModifierHelper.js';
import { UnsupportedEditorPanel } from '../UnsupportedElementEditor.js';
export const OperatorSelector = observer((props) => {
    const { setImplementation, isReadOnly } = props;
    const operationsOptions = Object.values(OperationType).map((value) => ({
        value,
        label: value,
    }));
    const selectedSourceType = {
        value: setImplementation.operation,
        label: setImplementation.operation,
    };
    const changeSourceType = (val) => {
        const value = val?.value ? OperationType[val.value] : undefined;
        if (value && setImplementation.operation !== value) {
            operationMapping_setOperation(setImplementation, value);
            operationMapping_setParameters(setImplementation, []);
        }
    };
    return (_jsx(CustomSelectorInput, { disabled: isReadOnly, options: operationsOptions, onChange: changeSourceType, value: selectedSourceType, placeholder: "Select operation ID" }));
});
export var CLASS_MAPPING_SOURCE_TYPE;
(function (CLASS_MAPPING_SOURCE_TYPE) {
    CLASS_MAPPING_SOURCE_TYPE["CLASS"] = "CLASS";
    CLASS_MAPPING_SOURCE_TYPE["FLAT_DATA"] = "FLAT DATA";
    CLASS_MAPPING_SOURCE_TYPE["OPERATION"] = "OPERATION";
    CLASS_MAPPING_SOURCE_TYPE["RELATIONAL"] = "DATABASE TABLE";
    CLASS_MAPPING_SOURCE_TYPE["RELATION_FUNCTION"] = "RELATION";
})(CLASS_MAPPING_SOURCE_TYPE || (CLASS_MAPPING_SOURCE_TYPE = {}));
export const ClassMappingEditor = observer((props) => {
    const { setImplementation, isReadOnly } = props;
    const editorStore = useEditorStore();
    const mappingEditorState = editorStore.tabManagerState.getCurrentEditorState(MappingEditorState);
    const _class = setImplementation.class;
    // ID
    const isDefaultId = fromElementPathToMappingElementId(_class.value.path) ===
        setImplementation.id.value;
    const showId = Object.keys(mappingEditorState.mappingElementsWithSimilarTarget).length >
        1;
    // Root
    const rootHasNotBeenSet = mappingEditorState.mappingElementsWithSimilarTarget.length === 1 &&
        setImplementation.root.value === false;
    // Source
    // TODO: Turn this into a packageable element
    let sourceType = '';
    let sourceName;
    if (setImplementation instanceof PureInstanceSetImplementation) {
        sourceType = CLASS_MAPPING_SOURCE_TYPE.CLASS;
        sourceName = setImplementation.srcClass?.value.name;
    }
    else if (setImplementation instanceof FlatDataInstanceSetImplementation) {
        sourceType = CLASS_MAPPING_SOURCE_TYPE.FLAT_DATA;
        sourceName = setImplementation.sourceRootRecordType.value._OWNER.name;
    }
    else if (setImplementation instanceof EmbeddedFlatDataPropertyMapping) {
        sourceType = CLASS_MAPPING_SOURCE_TYPE.FLAT_DATA;
        sourceName = setImplementation.rootInstanceSetImplementation.sourceRootRecordType.value._OWNER.name;
    }
    else if (setImplementation instanceof RootRelationalInstanceSetImplementation) {
        sourceType = CLASS_MAPPING_SOURCE_TYPE.RELATIONAL;
        sourceName = setImplementation.mainTableAlias?.relation.value.name;
    }
    else if (setImplementation instanceof RelationFunctionInstanceSetImplementation) {
        sourceType = CLASS_MAPPING_SOURCE_TYPE.RELATION_FUNCTION;
        sourceName = setImplementation.relationFunction?.name;
    }
    else if (setImplementation instanceof OperationSetImplementation) {
        sourceType = CLASS_MAPPING_SOURCE_TYPE.OPERATION;
    }
    else {
        const extraMappingSourceTypeInfoGetters = editorStore.pluginManager
            .getApplicationPlugins()
            .flatMap((plugin) => plugin.getExtraMappingSourceTypeInfoGetters?.() ?? []);
        for (const sourceTypeInfoGetter of extraMappingSourceTypeInfoGetters) {
            const mappingSourceTypeInfo = sourceTypeInfoGetter(setImplementation);
            if (mappingSourceTypeInfo) {
                sourceType = mappingSourceTypeInfo.sourceType;
                sourceName = mappingSourceTypeInfo.sourceName;
                break;
            }
        }
    }
    const toggleRoot = () => {
        if (!isReadOnly) {
            const isRoot = setImplementation.root.value;
            setImplementation_setRoot(setImplementation, !isRoot);
            if (setImplementation.root.value) {
                setImpl_nominateRoot(setImplementation);
            }
        }
    };
    return (_jsxs("div", { className: "mapping-element-editor class-mapping-editor", children: [_jsxs("div", { className: "mapping-element-editor__metadata", children: [_jsxs("div", { className: "mapping-element-editor__metadata__chunk mapping-element-editor__metadata__overview-chunk background--class", children: [_jsx("div", { className: "mapping-element-editor__metadata__sub-chunk", children: "class mapping" }), showId && (_jsx("div", { className: "mapping-element-editor__metadata__sub-chunk mapping-element-editor__metadata__overview__id", children: isDefaultId ? 'default ID' : setImplementation.id.value })), _jsx("div", { className: "mapping-element-editor__metadata__sub-chunk", children: "for" }), _jsxs("div", { className: "mapping-element-editor__metadata__sub-chunk mapping-element-editor__metadata__target", children: [_jsx("div", { className: "mapping-element-editor__metadata__target__type icon", children: _jsx(PURE_ClassIcon, {}) }), _jsx("div", { className: "mapping-element-editor__metadata__target__label", children: _class.value.name })] })] }), !(setImplementation instanceof OperationSetImplementation ||
                        setImplementation instanceof INTERNAL__UnknownSetImplementation) && (_jsxs("div", { className: clsx('mapping-element-editor__metadata__chunk', 'mapping-element-editor__metadata__source-chunk', {
                            'mapping-element-editor__metadata__source-chunk--none': !sourceName,
                        }), children: [_jsx("div", { className: "mapping-element-editor__metadata__sub-chunk", children: `with ${sourceName
                                    ? `source ${sourceType.toLowerCase()}`
                                    : `no source`}` }), sourceName && (_jsxs("div", { className: "mapping-element-editor__metadata__sub-chunk mapping-element-editor__metadata__source", children: [_jsx("div", { className: "mapping-element-editor__metadata__source__type icon", children: getElementTypeIcon(sourceType, editorStore) }), _jsx("div", { className: "mapping-element-editor__metadata__source__label", children: sourceName })] }))] })), setImplementation instanceof OperationSetImplementation && (_jsx("div", { className: "mapping-element-editor__metadata__operator-selector", children: _jsx(OperatorSelector, { setImplementation: setImplementation, isReadOnly: isReadOnly }) })), (showId || rootHasNotBeenSet) && (_jsxs("button", { className: clsx('mapping-element-editor__metadata__chunk', 'mapping-element-editor__metadata__root-chunk', {
                            'mapping-element-editor__metadata__root-chunk--checked': setImplementation.root.value,
                        }), onClick: toggleRoot, disabled: isReadOnly, tabIndex: -1, title: "Toggle set class mapping as root", children: [setImplementation.root.value ? (_jsx(CheckSquareIcon, {})) : (_jsx(EmptySquareIcon, {})), "root"] }))] }), setImplementation instanceof INTERNAL__UnknownSetImplementation && (_jsx(UnsupportedEditorPanel, { text: "Can't display this set implementation in form-mode", isReadOnly: isReadOnly })), setImplementation instanceof OperationSetImplementation && (_jsx(OperationSetImplementationEditor, { setImplementation: setImplementation, isReadOnly: isReadOnly })), !(setImplementation instanceof OperationSetImplementation ||
                setImplementation instanceof INTERNAL__UnknownSetImplementation) &&
                editorStore.graphManagerState.graphManager.isInstanceSetImplementation(setImplementation) && (_jsx(InstanceSetImplementationEditor, { setImplementation: setImplementation, isReadOnly: isReadOnly }))] }));
});
//# sourceMappingURL=ClassMappingEditor.js.map