import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } 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 { ResizablePanelGroup, ResizablePanel, PlusIcon, ResizablePanelSplitter, PanelFormTextField, PanelForm, ContextMenu, MenuContent, MenuContentItem, ControlledDropdownMenu, PanelListSelectorItem, BlankPanelContent, ResizablePanelSplitterLine, Panel, PanelHeader, PanelContent, PanelListSelectorItemLabel, PanelHeaderActions, } from '@finos/legend-art';
import { MapperPostProcessor, TableNameMapper, SchemaNameMapper, } from '@finos/legend-graph';
import { observer } from 'mobx-react-lite';
import { mapperPostProcessor_addMapper, mapperPostProcessor_deleteMapper, mapper_setFrom, mapper_setTo, schemaNameMapper_setFrom, schemaNameMapper_setTo, } from '../../../../../stores/graph-modifier/STO_Relational_GraphModifierHelper.js';
export const MapperPostProcessorEditor = observer((props) => {
    const { postProcessorState, isReadOnly, postProcessor } = props;
    const selectedMapper = postProcessorState.selectedMapper;
    const selectedSchemaNameMapper = selectedMapper instanceof TableNameMapper
        ? selectedMapper.schema
        : undefined;
    const selectMapper = (val) => {
        postProcessorState.setSelectedMapper(val);
    };
    const addSchemaMapper = () => {
        mapperPostProcessor_addMapper(postProcessor, new SchemaNameMapper('', ''));
        postProcessorState.setSelectedMapper(postProcessor.mappers.at(-1));
    };
    const addTableMapper = () => {
        mapperPostProcessor_addMapper(postProcessor, new TableNameMapper('', '', new SchemaNameMapper('', '')));
        postProcessorState.setSelectedMapper(postProcessor.mappers.at(-1));
    };
    const deleteMapper = (mapper) => () => {
        if (postProcessorState.postProcessor instanceof MapperPostProcessor) {
            mapperPostProcessor_deleteMapper(postProcessorState.postProcessor, mapper);
        }
        if (mapper === postProcessorState.selectedMapper) {
            postProcessorState.setSelectedMapper(undefined);
        }
    };
    const mappers = postProcessor.mappers;
    const isSchemaMapperDuplicated = (val) => mappers.filter((mapper) => mapper.from === val.from &&
        mapper.to === val.to &&
        val instanceof SchemaNameMapper &&
        mapper instanceof SchemaNameMapper).length >= 2;
    const isTableMapperDuplicated = (val) => mappers
        .filter((mapper) => val instanceof TableNameMapper && mapper instanceof TableNameMapper)
        .filter((mapper) => mapper.schema.from ===
        val.schema.from &&
        mapper.from ===
            val.from &&
        mapper.from === val.from).length >= 2;
    const isMapperDuplicated = (val) => isSchemaMapperDuplicated(val) || isTableMapperDuplicated(val);
    return (_jsx(ResizablePanelGroup, { orientation: "horizontal", children: _jsx(ResizablePanel, { children: _jsxs(ResizablePanelGroup, { orientation: "vertical", children: [_jsx(ResizablePanel, { size: 170, minSize: 70, children: _jsxs(Panel, { children: [_jsx(PanelHeader, { title: "mapper", children: _jsx(PanelHeaderActions, { children: _jsx(ControlledDropdownMenu, { title: "Create mapper", className: "panel__header__action", disabled: isReadOnly, content: _jsxs(MenuContent, { children: [_jsx(MenuContentItem, { onClick: addSchemaMapper, children: "New Schema Mapper" }), _jsx(MenuContentItem, { onClick: addTableMapper, children: "New Table Mapper" })] }), menuProps: {
                                                anchorOrigin: {
                                                    vertical: 'bottom',
                                                    horizontal: 'right',
                                                },
                                                transformOrigin: {
                                                    vertical: 'top',
                                                    horizontal: 'right',
                                                },
                                                elevation: 7,
                                            }, children: _jsx(PlusIcon, {}) }) }) }), _jsx(PanelContent, { children: mappers.map((mapper) => (_jsx(ContextMenu, { disabled: isReadOnly, content: _jsx(MenuContent, { children: _jsx(MenuContentItem, { disabled: isReadOnly, onClick: deleteMapper(mapper), children: "Delete" }) }), menuProps: { elevation: 7 }, children: _jsx(PanelListSelectorItem, { validationError: isMapperDuplicated(mapper), isSelected: mapper === postProcessorState.selectedMapper, onSelect: () => selectMapper(mapper), children: _jsx(PanelListSelectorItemLabel, { title: mapper instanceof TableNameMapper
                                                    ? 'Table Mapper'
                                                    : 'Schema Mapper', errorMessage: isMapperDuplicated(mapper)
                                                    ? 'Mappers have the same values'
                                                    : undefined }) }) }, mapper._UUID))) })] }) }), _jsx(ResizablePanelSplitter, { children: _jsx(ResizablePanelSplitterLine, { color: "var(--color-dark-grey-200)" }) }), _jsx(ResizablePanel, { children: _jsxs(Panel, { children: [_jsx(PanelHeader, { title: postProcessorState.selectedMapper instanceof TableNameMapper
                                        ? 'Table Mapper'
                                        : 'Schema Mapper' }), _jsxs(PanelContent, { children: [!selectedMapper && (_jsx(BlankPanelContent, { children: !mappers.length
                                                ? 'Add a mapper'
                                                : 'Choose a mapper to view' })), selectedMapper && (_jsxs(PanelForm, { children: [_jsx(PanelFormTextField, { isReadOnly: isReadOnly, value: selectedMapper.from, name: "From", update: (value) => mapper_setFrom(selectedMapper, value ?? '') }), _jsx(PanelFormTextField, { isReadOnly: isReadOnly, value: selectedMapper.to, name: "To", update: (value) => mapper_setTo(selectedMapper, value ?? '') }), selectedSchemaNameMapper && (_jsxs(_Fragment, { children: [_jsx(PanelFormTextField, { isReadOnly: isReadOnly, value: selectedSchemaNameMapper.from, name: "Schema - From", update: (value) => {
                                                                schemaNameMapper_setFrom(selectedSchemaNameMapper, value ?? '');
                                                            } }), _jsx(PanelFormTextField, { isReadOnly: isReadOnly, value: selectedSchemaNameMapper.to, name: "Schema - To", update: (value) => schemaNameMapper_setTo(selectedSchemaNameMapper, value ?? '') })] }))] }))] })] }) })] }) }) }));
});
//# sourceMappingURL=MapperPostProcessorEditor.js.map