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 { WithDisposable } from '@blockeditor/lit';
import { LitElement } from 'lit';
import { customElement, property } from 'lit/decorators.js';
import {} from '../../../../surface-block/managers/edit-session.js';
import {} from './common/create-popper.js';
let EdgelessToolButton = class EdgelessToolButton extends WithDisposable(LitElement) {
    constructor() {
        super(...arguments);
        this.active = false;
        this._menu = null;
    }
    _disposeMenu() {
        this._menu?.dispose();
        this._menu = null;
    }
    updateMenu() {
        this._states.forEach(key => {
            if (this._menu) {
                // @ts-ignore
                this._menu.element[key] = this[key];
            }
        });
    }
    get surface() {
        return this.edgeless.surface;
    }
    get service() {
        return this.surface.service;
    }
    connectedCallback() {
        super.connectedCallback();
        const { _disposables, edgeless, service } = this;
        const { editSession } = service;
        const attributes = editSession.getLastProps(this._type);
        this._states.forEach(key => {
            const value = attributes[key];
            if (value !== undefined)
                Object.assign(this, { [key]: value });
        });
        _disposables.add(this.edgeless.slots.edgelessToolUpdated.on(tool => {
            if (tool.type !== this._type) {
                this._disposeMenu();
            }
        }));
        this.initLastPropsSlot();
        edgeless.bindHotKey({
            Escape: () => {
                if (edgeless.edgelessTool.type === this._type) {
                    edgeless.tools.setEdgelessTool({ type: 'default' });
                }
            },
        }, { global: true });
    }
    initLastPropsSlot() {
        this._disposables.add(this.service.editSession.slots.lastPropsUpdated.on(({ type, props }) => {
            if (type === this._type) {
                this._states.forEach(_key => {
                    const key = _key;
                    if (props[key] != undefined) {
                        Object.assign(this, { [key]: props[key] });
                    }
                });
            }
        }));
    }
    disconnectedCallback() {
        this._disposeMenu();
        super.disconnectedCallback();
    }
};
__decorate([
    property({ attribute: false })
], EdgelessToolButton.prototype, "edgeless", void 0);
__decorate([
    property({ attribute: false })
], EdgelessToolButton.prototype, "active", void 0);
EdgelessToolButton = __decorate([
    customElement('edgeless-tool-button')
], EdgelessToolButton);
export { EdgelessToolButton };
//# sourceMappingURL=edgeless-toolbar-button.js.map