'use strict';

var obsidian = require('obsidian');

/*! *****************************************************************************
Copyright (c) Microsoft Corporation.

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
/* global Reflect, Promise */

var extendStatics = function(d, b) {
    extendStatics = Object.setPrototypeOf ||
        ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
        function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
    return extendStatics(d, b);
};

function __extends(d, b) {
    if (typeof b !== "function" && b !== null)
        throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
    extendStatics(d, b);
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
}

var CycleThroughPanes = /** @class */ (function (_super) {
    __extends(CycleThroughPanes, _super);
    function CycleThroughPanes() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.lastPanes = [];
        return _this;
    }
    CycleThroughPanes.prototype.onload = function () {
        var _this = this;
        console.log('loading plugin: Cycle through panes');
        this.addCommand({
            id: 'cycle-through-panes',
            name: 'Cycle through Panes',
            checkCallback: function (checking) {
                var active = _this.app.workspace.activeLeaf;
                if (active) {
                    if (!checking) {
                        var leafs = _this.app.workspace.getLeavesOfType("markdown");
                        var index = leafs.indexOf(active);
                        if (index === leafs.length - 1) {
                            _this.app.workspace.setActiveLeaf(leafs[0], true, true);
                        }
                        else {
                            _this.app.workspace.setActiveLeaf(leafs[index + 1], true, true);
                        }
                    }
                    return true;
                }
                return false;
            }, hotkeys: [
                {
                    modifiers: ["Ctrl"],
                    key: "Tab"
                }
            ]
        });
        this.addCommand({
            id: 'cycle-through-panes-reverse',
            name: 'Cycle through panes (Reverse)',
            checkCallback: function (checking) {
                var active = _this.app.workspace.activeLeaf;
                if (active) {
                    if (!checking) {
                        var leafs = _this.app.workspace.getLeavesOfType("markdown");
                        var index = leafs.indexOf(active);
                        if (index) {
                            if (index === 0) {
                                _this.app.workspace.setActiveLeaf(leafs[leafs.length - 1], true, true);
                            }
                            else {
                                _this.app.workspace.setActiveLeaf(leafs[index - 1], true, true);
                            }
                        }
                    }
                    return true;
                }
                return false;
            }, hotkeys: [
                {
                    modifiers: ["Ctrl", "Shift"],
                    key: "Tab"
                }
            ]
        });
        //fires when a new file is opened or the focus switches to another pane
        this.app.workspace.on("file-open", function () {
            var _a;
            //use just markdown panes
            if (_this.app.workspace.activeLeaf.getViewState().type != "markdown") {
                return;
            }
            //if a file gets opened in current pane
            if (((_a = _this.lastPanes) === null || _a === void 0 ? void 0 : _a.last()) == _this.app.workspace.activeLeaf.id) {
                return;
            }
            //keep a history of 10 panes
            if (_this.lastPanes.length > 10) {
                _this.lastPanes.splice(0, 1);
            }
            //add current pane to history
            _this.lastPanes.push(_this.app.workspace.activeLeaf.id);
        });
        this.addCommand({
            id: 'focus-on-last-active-pane',
            name: 'Focus on last active pane',
            callback: function () {
                var leaf;
                //Cycle thorough the history until a pane is still there and not the current pane
                for (var i = 2; i <= _this.lastPanes.length; i++) {
                    if (_this.lastPanes[_this.lastPanes.length - i] == _this.lastPanes.last())
                        continue;
                    leaf = _this.app.workspace.getLeafById(_this.lastPanes[_this.lastPanes.length - i]);
                    if (leaf)
                        break;
                }
                if (leaf) {
                    _this.app.workspace.setActiveLeaf(leaf, true, true);
                }
            }
        });
    };
    CycleThroughPanes.prototype.onunload = function () {
        console.log('unloading plugin: Cycle through panes');
    };
    return CycleThroughPanes;
}(obsidian.Plugin));

module.exports = CycleThroughPanes;
//# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 26460 characters]
