import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import React from "react";
import styled from "styled-components";
import { DragDropContext } from "react-beautiful-dnd";
import { XMappingDestination } from "./Components/XMappingDestination";
import { XMappingRules } from "./Components/XMappingRules";
import { XMappingSource } from "./Components/XMappingSource";
import { MappingEditContext } from "./MappingEditContext";
import { XMappingConstants } from "./XMappingConstants";
import { StateManager } from "../StateManagement/StateManager";
import { Xwrapper } from "@appglue/xarrow/lib";
export class XMappingEditor extends React.Component {
    constructor(props) {
        super(props);
        this.onDragEnd = (result) => {
            if (!result.destination)
                return;
            // mapping rule to destination
            if (result.source.droppableId.startsWith(XMappingConstants.MAPPING_PREFIX_RULE_ITEM)) {
                let mappingRule = this.props.definition.find(result.draggableId);
                if (!mappingRule)
                    return;
                let destinationPath = result.destination.droppableId.replace(XMappingConstants.MAPPING_PREFIX_DESTINATION_ITEM, "");
                try {
                    this.props.definition.setRuleTarget(mappingRule, destinationPath);
                    StateManager.changed(mappingRule);
                    StateManager.propertyChanged(this.props.definition, "mappingRules");
                }
                catch (e) {
                    console.log("incorrect destination path:", result.destination.droppableId);
                }
            }
            // source to destination
            if (result.source.droppableId.startsWith(XMappingConstants.MAPPING_PREFIX_SOURCE_ITEM) &&
                result.destination.droppableId.startsWith(XMappingConstants.MAPPING_PREFIX_DESTINATION_ITEM)) {
                try {
                    let sourcePath = result.draggableId.replace(XMappingConstants.MAPPING_PREFIX_SOURCE_ITEM, "");
                    let destinationPath = result.destination.droppableId.replace(XMappingConstants.MAPPING_PREFIX_DESTINATION_ITEM, "");
                    this.props.definition.directConnect(sourcePath, destinationPath);
                    StateManager.propertyChanged(this.props.definition, "mappingRules");
                }
                catch (e) {
                    console.log("incorrect connection:", result.destination.droppableId, result.draggableId);
                }
            }
            // drag and drop on expressions
            if (result.draggableId.startsWith(XMappingConstants.MAPPING_PREFIX_DESTINATION_ITEM)) {
                let destinationPath = result.draggableId.replace(XMappingConstants.MAPPING_PREFIX_DESTINATION_ITEM, "");
                // from destination to create new one
                if (result.destination.droppableId === XMappingConstants.MAPPING_RULES_DROPPABLE_ID) {
                    try {
                        this.props.definition.addMappingRuleFromDestination(destinationPath);
                        StateManager.propertyChanged(this.props.definition, "mappingRules");
                    }
                    catch (e) {
                        console.log("incorrect connection:", result.destination.droppableId, result.draggableId);
                    }
                }
                // from destination to source
                if (result.destination.droppableId.startsWith(XMappingConstants.MAPPING_PREFIX_SOURCE_ITEM)) {
                    try {
                        let sourcePath = result.destination.droppableId.replace(XMappingConstants.MAPPING_PREFIX_SOURCE_ITEM, "");
                        this.props.definition.directConnect(sourcePath, destinationPath);
                        StateManager.propertyChanged(this.props.definition, "mappingRules");
                    }
                    catch (e) {
                        console.log("incorrect connection:", result.destination.droppableId, result.draggableId);
                    }
                }
                // from destination to rule
                if (result.destination.droppableId.startsWith(XMappingConstants.MAPPING_PREFIX_RULE_ITEM)) {
                    let mappingRuleId = result.destination.droppableId.replace(XMappingConstants.MAPPING_PREFIX_RULE_ITEM, "");
                    let mappingRule = this.props.definition.find(mappingRuleId);
                    if (!mappingRule)
                        return;
                    try {
                        this.props.definition.setRuleTarget(mappingRule, destinationPath);
                        StateManager.changed(mappingRule);
                        StateManager.propertyChanged(this.props.definition, "mappingRules");
                    }
                    catch (e) {
                        console.log("incorrect destination path:", result.destination.droppableId);
                    }
                }
            }
        };
        this.editContext = new MappingEditContext(props.definition);
    }
    render() {
        const { definition } = this.props;
        return (_jsx(DragDropContext, Object.assign({ onDragEnd: this.onDragEnd }, { children: _jsx(XMappingEditorRender, { children: _jsxs(Xwrapper, { children: [_jsx(XMappingSource, { dataDefinition: definition.getInputDefinition(), editContext: this.editContext }, void 0),
                        _jsx(XMappingRules, { definition: definition, editContext: this.editContext }, void 0),
                        _jsx(XMappingDestination, { editContext: this.editContext, definition: definition }, void 0)] }, void 0) }, void 0) }), void 0));
    }
}
const XMappingEditorRender = styled.div `
    width: 100%;
    height: 100vh;
    display: flex;
    flex-direction: row;
    background: #e7e7e7;
`;
//# sourceMappingURL=XMappingEditor.js.map