export default class ArenaSelection {
    constructor(startNode, startOffset, endNode, endOffset, direction) {
        this.startNode = startNode;
        this.startOffset = startOffset;
        this.endNode = endNode;
        this.endOffset = endOffset;
        this.direction = direction;
    }
    clone() {
        return new ArenaSelection(this.startNode, this.startOffset, this.endNode, this.endOffset, this.direction);
    }
    setStartNode(startNode, startOffset) {
        this.startNode = startNode;
        this.startOffset = startOffset;
        return this;
    }
    setEndNode(endNode, endOffset) {
        this.endNode = endNode;
        this.endOffset = endOffset;
        return this;
    }
    setBoth(node, offset) {
        return this.setStartNode(node, offset).setEndNode(node, offset);
    }
    getCursor() {
        return {
            node: this.direction === 'forward' ? this.endNode : this.startNode,
            offset: this.direction === 'forward' ? this.endOffset : this.startOffset,
        };
    }
    setCursor(cursor) {
        return this.setBoth(cursor.node, cursor.offset);
    }
    isCollapsed() {
        return this.startNode === this.endNode && this.startOffset === this.endOffset;
    }
    isSameNode() {
        return this.startNode === this.endNode;
    }
    isSelectionOnBegin() {
        return this.startOffset === 0;
    }
    isSelectionOnEnd() {
        return this.endNode.getTextLength() === this.endOffset;
    }
    collapse() {
        if (this.startNode === this.endNode) {
            this.endOffset = this.startOffset;
            return this;
        }
        if (this.direction === 'forward') {
            this.startNode = this.endNode;
            this.endOffset = 0;
            this.startOffset = 0;
        }
        else {
            this.endNode = this.startNode;
            this.endOffset = this.startOffset;
        }
        return this;
    }
    collapseBackward() {
        this.endNode = this.startNode;
        this.endOffset = this.startOffset;
        return this;
    }
    trim() {
        if (this.isCollapsed()) {
            return this;
        }
        const textA = this.startNode.getRawText().slice(this.startOffset);
        const matchA = textA.match(/^( +)/g);
        if (matchA) {
            const len = matchA[0].length;
            if (this.startNode !== this.endNode) {
                this.startOffset += len;
            }
            else {
                this.startOffset = Math.min(this.startOffset + len, this.endOffset);
            }
        }
        const textB = this.endNode.getRawText().slice(0, this.endOffset);
        const matchB = textB.match(/( +)$/g);
        if (matchB) {
            const len = matchB[0].length;
            if (this.startNode !== this.endNode) {
                this.endOffset -= len;
            }
            else {
                this.endOffset = Math.max(this.endOffset - len, this.startOffset);
            }
        }
        return this;
    }
}
//# sourceMappingURL=ArenaSelection.js.map