import { PositionData } from "./MappingEditContext.PositionData";
import { HTMLShapeGeometry, HTMLUtilities } from "../HTMLUtilities";
import { XMappingConstants } from "./XMappingConstants";
import { MappingRuleViewType } from "./XMappingDefinition";
import { ReactRenderHook } from "../ReactRenderHook";
import { StateManager } from "../StateManagement/StateManager";
export class MappingEditContext {
    constructor(definition) {
        this.definition = definition;
        this.definition.editContext = this;
    }
    setSelectedRule(mappingRule) {
        this._selected = mappingRule.id;
    }
    get selected() {
        return this._selected;
    }
    set selectedSource(sel) {
        this._selectedSource = sel;
    }
    get selectedSource() {
        return this._selectedSource;
    }
    set selectedDestination(sel) {
        this._selectedDestination = sel;
    }
    get selectedDestination() {
        return this._selectedDestination;
    }
    // on events
    onMappingRuleMove() {
        this.delayPosition();
    }
    onAddMappingRule() {
        this.delayPosition();
    }
    onMappingRuleViewChanged() {
        this.delayPosition();
    }
    positionMappingRules() {
        let workingSet = [];
        // add mapping rules to this
        for (let realS of this.definition.mappingRules) {
            if (realS.viewType === MappingRuleViewType.HIDDEN) {
                continue;
            }
            let geo = HTMLUtilities.getGeometry(realS.id);
            if (!geo) {
                geo = new HTMLShapeGeometry(realS.x, realS.y, XMappingConstants.DEFAULT_RULE_WIDTH, XMappingConstants.DEFAULT_RULE_HEIGHT);
            }
            geo.topPadding = XMappingConstants.ACTION_AREA_HEIGHT;
            geo.bottomPadding = XMappingConstants.SPACE_Y_BETWEEN_RULES;
            geo.rightPadding = XMappingConstants.SPACE_X_BETWEEN_RULES;
            geo.top = realS.desiredY;
            geo.left = realS.desiredX;
            workingSet.push(new PositionData(geo, false, realS, realS.x, realS.y));
        }
        // sort working set
        workingSet = workingSet.sort((a, b) => {
            // sort real above candidates
            if (a.shape.top + a.shape.left > b.shape.top + b.shape.left) {
                return 1;
            }
            else {
                return -1;
            }
        });
        for (let positionThis of workingSet) {
            // find any that are in vertical range
            let consider = [];
            // console.log('positioning', positionThis)
            for (let otherPostion of workingSet) {
                if (!otherPostion.positioned)
                    continue;
                if (positionThis.inRange(otherPostion))
                    consider.push(otherPostion);
            }
            // position so its missed
            PositionData.positionOne(positionThis, consider);
        }
        for (let o of workingSet) {
            if (o.initialX !== o.mappingRule.x || o.initialY !== o.mappingRule.y) {
                return true;
            }
        }
        return false;
    }
    refresh() {
        StateManager.propertyChanged(this.definition, "mappingRules");
    }
    delayPosition(alwaysRefresh = false) {
        ReactRenderHook.registerCallback("position", () => {
            if (this.positionMappingRules()) {
                this.refresh();
            }
            if (alwaysRefresh)
                this.refresh();
        });
    }
}
//# sourceMappingURL=MappingEditContext.js.map