/*
 * Copyright (C) 2016-2020 Kano Computing Ltd.
 * License: http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
 */
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { RemixFloatingMenu } from './widget/floating-menu.js';
import { registerRemix, Remix } from '../../../remix/index.js';
import { button } from '@kano/styles/button.js';
import { _ } from '../../../i18n/index.js';
import { debounce } from '../../../decorators.js';
export class BlocklyRemix extends Remix {
    getResetConfirm() {
        if (!this.resetConfirm) {
            this.resetConfirm = this.editor.dialogs.registerConfirm({
                buttonLabel: _('DIALOG_RESET_REMIX_LABEL', 'Confirm'),
                heading: _('DIALOG_RESET_REMIX_HEADING', 'Are you sure you want to reset your remix?'),
                text: _('DIALOG_RESET_REMIX_TEXT', 'You will lose all your changes'),
            });
            this.resetConfirm.onDidConfirm(() => this.reset());
        }
        return this.resetConfirm;
    }
    reset() {
        if (!this.data) {
            return;
        }
        this.editor.load(this.data.app);
        this.editor.setCode(this.data.app.code);
    }
    start() {
        super.start();
        this.editor.domNode.shadowRoot.appendChild(button.content.cloneNode(true));
        if (!this.data) {
            return;
        }
        this.menu = new RemixFloatingMenu(this.data.instruction, this.data.suggestions, this.data.nextChallengeButton ? this.data.nextChallengeButton : false);
        this.menu.setTitle(this.data.title || _('REMIX', 'Remix'));
        this.menu.addEntry();
        if (this.data.icon) {
            const domNode = this.data.icon.getDomNode();
            this.menu.setIconNode(domNode);
        }
        this.menu.onDidSelectSuggestion((s) => this.selectSuggestion(s));
        this.menu.onDidDeselectSuggestion(() => super.deselectSuggestion());
        this.menu.onDidRequestReset(() => {
            this.deselectSuggestion();
            const dialog = this.getResetConfirm();
            dialog.open();
        });
        this.menu.onDidRequestExamples(() => {
            if (!this.dialog) {
                return;
            }
            this.dialog.open();
        });
        this.menu.onDidEnd(() => this._onDidEnd.fire());
        this.menu.onDidRequestNextChallenge(() => this._onDidRequestNextChallenge.fire());
        this.editor.addContentWidget(this.menu);
    }
    selectSuggestion(suggestion) {
        super.selectSuggestion(suggestion);
        const pos = this.editor.queryPosition(suggestion.target);
        if (pos.isBlock) {
            // @ts-ignore
            const workspace = this.editor.sourceEditor.domNode.getBlocklyWorkspace();
            const block = this.editor.querySelector(suggestion.target);
            if (block) {
                workspace.centerOnBlock(block.getId());
            }
        }
    }
    deselectSuggestion() {
        super.deselectSuggestion();
        if (this.menu) {
            this.menu.deselectSuggestion();
        }
    }
    dispose() {
        super.dispose();
        if (this.resetConfirm) {
            this.resetConfirm.dispose();
        }
    }
}
__decorate([
    debounce(100)
], BlocklyRemix.prototype, "selectSuggestion", null);
registerRemix('blockly', BlocklyRemix);
//# sourceMappingURL=index.js.map