/**
* 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 React, { useRef } from 'react';
import { LEGEND_STUDIO_TEST_ID } from '../../../__lib__/LegendStudioTesting.js';
import { observer } from 'mobx-react-lite';
import {
NewPackageableRuntimeDriver,
NewPackageableConnectionDriver,
NewPureModelConnectionDriver,
NewFileGenerationDriver,
resolvePackageAndElementName,
NewDataElementDriver,
NewServiceDriver,
CONNECTION_TYPE,
type RuntimeOption,
NewLakehouseDataProductDriver,
NewRuntimeType,
} from '../../../stores/editor/NewElementState.js';
import {
Dialog,
compareLabelFn,
CustomSelectorInput,
PanelBooleanField,
} from '@finos/legend-art';
import type { EditorStore } from '../../../stores/editor/EditorStore.js';
import {
guaranteeNonNullable,
prettyCONSTName,
toTitleCase,
} from '@finos/legend-shared';
import type { DSL_LegendStudioApplicationPlugin_Extension } from '../../../stores/LegendStudioApplicationPlugin.js';
import { useEditorStore } from '../EditorStoreProvider.js';
import {
type Mapping,
type Store,
type Class,
ELEMENT_PATH_DELIMITER,
} from '@finos/legend-graph';
import { flowResult } from 'mobx';
import { useApplicationStore } from '@finos/legend-application';
import {
buildElementOption,
getPackageableElementOptionFormatter,
type PackageableElementOption,
} from '@finos/legend-lego/graph-editor';
import type { EmbeddedDataTypeOption } from '../../../stores/editor/editor-state/element-editor-state/data/DataEditorState.js';
import type { DSL_Data_LegendStudioApplicationPlugin_Extension } from '../../../stores/extensions/DSL_Data_LegendStudioApplicationPlugin_Extension.js';
import { PACKAGEABLE_ELEMENT_TYPE } from '../../../stores/editor/utils/ModelClassifierUtils.js';
import { EmbeddedDataType } from '../../../stores/editor/editor-state/ExternalFormatState.js';
import type { DSL_Mapping_LegendStudioApplicationPlugin_Extension } from '../../../stores/extensions/DSL_Mapping_LegendStudioApplicationPlugin_Extension.js';
import type { GenerationTypeOption } from '../../../stores/editor/editor-state/GraphGenerationState.js';
export const getElementTypeLabel = (
editorStore: EditorStore,
type: string | undefined,
): string | undefined => {
switch (type) {
case PACKAGEABLE_ELEMENT_TYPE.PACKAGE:
case PACKAGEABLE_ELEMENT_TYPE.CLASS:
case PACKAGEABLE_ELEMENT_TYPE.ENUMERATION:
case PACKAGEABLE_ELEMENT_TYPE.ASSOCIATION:
case PACKAGEABLE_ELEMENT_TYPE.MEASURE:
case PACKAGEABLE_ELEMENT_TYPE.PROFILE:
case PACKAGEABLE_ELEMENT_TYPE.FUNCTION:
case PACKAGEABLE_ELEMENT_TYPE.MAPPING:
case PACKAGEABLE_ELEMENT_TYPE.CONNECTION:
case PACKAGEABLE_ELEMENT_TYPE.RUNTIME:
case PACKAGEABLE_ELEMENT_TYPE.SERVICE:
return type.toLowerCase();
case PACKAGEABLE_ELEMENT_TYPE.FLAT_DATA_STORE:
return 'flat-data store';
case PACKAGEABLE_ELEMENT_TYPE.DATABASE:
return 'relational database';
case PACKAGEABLE_ELEMENT_TYPE.SERVICE_STORE:
return 'service store';
case PACKAGEABLE_ELEMENT_TYPE.FILE_GENERATION:
return 'file generation';
case PACKAGEABLE_ELEMENT_TYPE.GENERATION_SPECIFICATION:
return 'generation specification';
case PACKAGEABLE_ELEMENT_TYPE.DATA:
return 'data';
case PACKAGEABLE_ELEMENT_TYPE.TEMPORARY__LOCAL_CONNECTION:
return 'local connection';
case PACKAGEABLE_ELEMENT_TYPE._DATA_PRODUCT:
return 'Lakehouse Data Product';
case PACKAGEABLE_ELEMENT_TYPE._COMPUTE:
return 'Compute';
default: {
if (type) {
const extraElementTypeLabelGetters = editorStore.pluginManager
.getApplicationPlugins()
.flatMap(
(plugin) =>
(
plugin as DSL_LegendStudioApplicationPlugin_Extension
).getExtraElementTypeLabelGetters?.() ?? [],
);
for (const typeLabelGetter of extraElementTypeLabelGetters) {
const label = typeLabelGetter(type);
if (label) {
return label;
}
}
return type.toLowerCase();
}
return undefined;
}
}
};
interface ElementTypeSelectOption {
label: string;
value: string;
}
const buildElementTypeOption = (type: string): ElementTypeSelectOption => ({
label: type,
value: type,
});
const NewDataElementDriverEditor = observer(() => {
const editorStore = useEditorStore();
const applicationStore = editorStore.applicationStore;
const newDataELementDriver =
editorStore.newElementState.getNewElementDriver(NewDataElementDriver);
const selectedOption = newDataELementDriver.embeddedDataOption
? {
label: prettyCONSTName(newDataELementDriver.embeddedDataOption.label),
value: newDataELementDriver.embeddedDataOption.value,
}
: undefined;
const extraOptionTypes = editorStore.pluginManager
.getApplicationPlugins()
.flatMap(
(plugin) =>
(
plugin as DSL_Data_LegendStudioApplicationPlugin_Extension
).getExtraEmbeddedDataTypeOptions?.() ?? [],
);
let options: EmbeddedDataTypeOption[] = Object.values(EmbeddedDataType)
.filter((type) => type !== EmbeddedDataType.DATA_ELEMENT)
.map((typeOption) => ({
label: prettyCONSTName(typeOption),
value: typeOption,
}));
options = options.concat(extraOptionTypes);
const onTypeSelectionChange = (val: EmbeddedDataTypeOption | null): void => {
if (!val) {
newDataELementDriver.setEmbeddedDataOption(undefined);
} else {
newDataELementDriver.setEmbeddedDataOption(val);
}
};
return (