"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MappingEditContext = void 0;
const MappingBuilderPage_1 = require("./MappingBuilderPage");
const MappingConstants_1 = require("@appglue/common/dist/Mapping/MappingConstants");
const StateManager_1 = require("@appglue/common/dist/Common/StateManager");
class MappingEditContext {
    definition;
    mode = MappingBuilderPage_1.MappingBuilderPage.Designer;
    isDragging = false;
    mappingBuilderName;
    onSave;
    onCancel;
    designConfig;
    runtimeConfig;
    clipboardElement;
    lastSelectionElement;
    _collapsedDestinationPaths = [];
    _collapsedSourcePaths = [];
    _highlightedRules = [];
    constructor(definition) {
        this.definition = definition;
    }
    _selectedDestination;
    get selectedDestination() {
        return this._selectedDestination;
    }
    set selectedDestination(sel) {
        this._selectedDestination = sel;
    }
    set hightlightedRules(ruleIds) {
        if (this.isDragging)
            return;
        this._highlightedRules = ruleIds;
        StateManager_1.StateManager.changed(this);
    }
    set hoveredSource(path) {
        if (this.isDragging)
            return;
        if (path) {
            let rules = this.definition.getMappingRulesByInputPath(path);
            this._highlightedRules = rules.map((r) => r.id);
        }
        else {
            this._highlightedRules = [];
        }
        StateManager_1.StateManager.changed(this);
    }
    _hoveredExpression;
    get hoveredExpression() {
        return this._hoveredExpression;
    }
    set hoveredExpression(id) {
        if (this.isDragging)
            return;
        if (id === this._hoveredExpression)
            return;
        if (id) {
            let mappingRule = this.definition.find(id);
            if (!mappingRule || (mappingRule && mappingRule.empty))
                return;
            this._highlightedRules = [id];
        }
        else {
            this._highlightedRules = [];
        }
        this._hoveredExpression = id;
        StateManager_1.StateManager.changed(this);
    }
    toggleDestinationCollapse(path) {
        let idx = this._collapsedDestinationPaths.indexOf(path);
        if (idx < 0) {
            this._collapsedDestinationPaths.push(path);
        }
        else {
            this._collapsedDestinationPaths.splice(idx, 1);
        }
        StateManager_1.StateManager.changed(this);
    }
    isDestinationCollapsed(path, forChildren = true) {
        let pieces = path.split(".");
        if (forChildren) {
            for (let i = 1; i < pieces.length; i++) {
                let anchestorPath = pieces.slice(0, i).join(".");
                if (this._collapsedDestinationPaths.includes(anchestorPath))
                    return true;
            }
        }
        else {
            return this._collapsedDestinationPaths.includes(path);
        }
        return false;
    }
    toggleSourceCollapse(path) {
        let idx = this._collapsedSourcePaths.indexOf(path);
        if (idx < 0) {
            this._collapsedSourcePaths.push(path);
        }
        else {
            this._collapsedSourcePaths.splice(idx, 1);
        }
        StateManager_1.StateManager.changed(this);
    }
    isSourceCollapsed(path, forChildren = true) {
        let pieces = path.split(".");
        if (forChildren) {
            for (let i = 1; i < pieces.length; i++) {
                let anchestorPath = pieces.slice(0, i).join(".");
                if (this._collapsedSourcePaths.includes(anchestorPath))
                    return true;
            }
        }
        else {
            return this._collapsedSourcePaths.includes(path);
        }
        return false;
    }
    isHighlighted(ruleId) {
        return this._highlightedRules.includes(ruleId);
    }
    isHighlightedSource(path) {
        let rules = this.definition.getMappingRulesByInputPath(path);
        if (this._highlightedRules.length === 0)
            return false;
        for (let rule of rules) {
            if (this._highlightedRules.includes(rule.id)) {
                return true;
            }
        }
        return false;
    }
    // for ui
    sourceId(path) {
        return `${MappingConstants_1.MappingConstants.MAPPING_PREFIX_SOURCE_ITEM}${path}`;
    }
    sourceConnectorIdForParent(path) {
        return `${MappingConstants_1.MappingConstants.MAPPING_PREFIX_SOURCE_ITEM}${path}_connector_forparent`;
    }
    sourceConnectorIdForChildren(path) {
        return `${MappingConstants_1.MappingConstants.MAPPING_PREFIX_SOURCE_ITEM}${path}_connector_forchildren`;
    }
    sourceDragId(path) {
        return `${this.sourceId(path)}${MappingConstants_1.MappingConstants.DRAGGING_CONNECTOR_SUFFIX}`;
    }
    ruleId(id) {
        return `${MappingConstants_1.MappingConstants.MAPPING_PREFIX_RULE_ITEM}${id}`;
    }
    ruleIdForSource(id) {
        return `${MappingConstants_1.MappingConstants.MAPPING_PREFIX_RULE_ITEM}${id}_connector_forsource`;
    }
    ruleIdForDestination(id) {
        return `${MappingConstants_1.MappingConstants.MAPPING_PREFIX_RULE_ITEM}${id}_connector_fordestination`;
    }
    destinationId(path) {
        return `${MappingConstants_1.MappingConstants.MAPPING_PREFIX_DESTINATION_ITEM}${path}`;
    }
    destinationConnectorIdForParent(path) {
        return `${MappingConstants_1.MappingConstants.MAPPING_PREFIX_SOURCE_ITEM}${path}_connector_forparent`;
    }
    destinationConnectorIdForChildren(path) {
        return `${MappingConstants_1.MappingConstants.MAPPING_PREFIX_SOURCE_ITEM}${path}_connector_forchildren`;
    }
    destinationDragId(id) {
        return `${MappingConstants_1.MappingConstants.MAPPING_PREFIX_RULE_ITEM}${id}${MappingConstants_1.MappingConstants.DRAGGING_CONNECTOR_SUFFIX}`;
    }
    // eslint-disable-next-line
    onCopy(id) { }
    // eslint-disable-next-line
    onCut(id) { }
    // eslint-disable-next-line
    onDelete(id) { }
    // eslint-disable-next-line
    onPaste(id) { }
}
exports.MappingEditContext = MappingEditContext;
//# sourceMappingURL=MappingEditContext.js.map