import React from "react";
import { Tool } from "../structures/editor/tools/tool";
import { ToolBar } from "./toolbar";

export class Editor extends React.Component {
    state: {
        currentTool: Tool;
        previousTool: Tool;
    };

    constructor(props: any) {
        super(props);
    }

    /**
     * Sets current tool. Select events not triggered if already selected.
     * @param tool New tool to select.
     */
    public setTool(tool: Tool) {
        if (this.state.currentTool == tool) {
            return;
        }

        this.setState({ currentTool: tool });
    }

    /**
     * If exists, sets previously selected tool as current tool.
     */
    public setPreviousTool() {
        if (this.state.previousTool === undefined) {
            return;
        }

        this.setTool(this.state.previousTool);
    }

    /**
     * @returns Tool that is currently selected.
     */
    public getCurrentTool(): Tool {
        return this.state.currentTool;
    }

    render(): React.ReactNode {
        return (
            <div id="ks-editor">
                <ToolBar editor={this}></ToolBar>
            </div>
        );
    }
}