"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var electron_1 = require("electron");
Polymer({
    is: 'toc-dialog',
    properties: {
        opened: {
            type: Boolean,
            value: false,
        },
        currentItems: {
            type: Array,
            value: [],
        },
        currentOutline: {
            type: Array,
            value: [],
        },
        selectedIdx: Number,
        innerDialog: Object,
        scrollCallback: Object,
        reset: {
            type: Object,
            value: function () { return this._unmount; },
        },
        onMount: Object,
        onUnmount: Object,
    },
    open: function (outline) {
        var _this = this;
        if (!this.innerDialog) {
            return;
        }
        if (outline.length > 0) {
            var elems = outline.map(function (h, i) {
                var outer = document.createElement('paper-item');
                var header = document.createElement('h' + h.level);
                header.innerHTML = '#'.repeat(h.level) + " " + h.html;
                outer.addEventListener('click', function () { return _this.selectItem(i); });
                outer.className = 'toc-section-item';
                outer.style.cursor = 'pointer';
                outer.appendChild(header);
                return outer;
            });
            var listbox = document.getElementById('toc-listbox');
            while (listbox.firstChild) {
                listbox.removeChild(listbox.firstChild);
            }
            for (var _i = 0, elems_1 = elems; _i < elems_1.length; _i++) {
                var e = elems_1[_i];
                listbox.appendChild(e);
            }
            this.currentItems = elems;
            this.currentOutline = outline;
            this.selectedIdx = -1;
        }
        this.innerDialog.open();
        this.opened = true;
        if (this.onMount) {
            this.onMount();
        }
    },
    selectItem: function (idx) {
        if (idx !== undefined && 0 <= idx && idx < this.currentItems.length) {
            if (this.scrollCallback) {
                this.scrollCallback(this.currentOutline[idx]);
            }
        }
        this.close();
    },
    _unmount: function () {
        this.opened = false;
        this.selectedIdx = undefined;
        this.currentItems = [];
        this.currentOutline = [];
        if (this.onUnmount) {
            this.onUnmount();
        }
    },
    close: function () {
        if (!this.innerDialog) {
            return;
        }
        this.innerDialog.close();
        this._unmount();
    },
    toggle: function (outline) {
        if (outline === void 0) { outline = []; }
        if (this.opened) {
            this.close();
        }
        else {
            this.open(outline);
        }
    },
    focusNext: function () {
        if (this.selectedIdx === undefined || this.currentItems.length === 0) {
            return;
        }
        ++this.selectedIdx;
        if (this.selectedIdx >= this.currentItems.length) {
            this.selectedIdx = 0;
        }
        this.currentItems[this.selectedIdx].focus();
    },
    focusPrevious: function () {
        if (this.selectedIdx === undefined || this.currentItems.length === 0) {
            return;
        }
        --this.selectedIdx;
        if (this.selectedIdx < 0) {
            this.selectedIdx = this.currentItems.length - 1;
        }
        this.currentItems[this.selectedIdx].focus();
    },
    copyOutlineToClipboard: function () {
        if (this.currentItems.length > 0) {
            var headings = [];
            for (var _i = 0, _a = this.currentOutline; _i < _a.length; _i++) {
                var h = _a[_i];
                headings.push(' '.repeat(h.level) + "- [" + h.title + "](#" + h.hash + ")");
            }
            electron_1.clipboard.writeText(headings.join('\n'));
        }
        this.close();
    },
    ready: function () {
        var _this = this;
        this.innerDialog = document.getElementById('toc-body');
        var button = document.getElementById('toc-copy-to-clipboard-button');
        button.addEventListener('click', function () { return _this.copyOutlineToClipboard(); });
        document.getElementById('toc-body').addEventListener('keydown', function (event) {
            switch (event.code) {
                case 'Enter':
                    _this.selectItem(_this.selectedIdx);
                    break;
                case 'KeyJ':
                    _this.focusNext();
                    break;
                case 'KeyK':
                    _this.focusPrevious();
                    break;
                case 'Escape':
                    _this.close();
                    break;
                case 'KeyG':
                    if (event.ctrlKey) {
                        _this.close();
                    }
                    break;
                case 'KeyC':
                    if (event.ctrlKey) {
                        _this.copyOutlineToClipboard();
                    }
                default:
                    break;
            }
        });
    },
});
//# sourceMappingURL=toc-dialog.js.map