import { LitElement, PropertyValues, css, html } from "lit";
import { styles } from "../../styles/shared-styles";



export class MenuBar extends LitElement {

    static properties = {
        items: { type: Array }, // Array of menu items
        editorView: { type: Object }, // Reference to the editor view
      };

      constructor() {
        super();
        this.items = [];
        this.editorView = null;
      }

    static stylse = [styles, css `
    .menubar {
      display: flex;
      gap: 10px;
      padding: 5px;
      background-color: #f9f9f9;
      border-bottom: 1px solid #ddd;
    }
    .menubar button {
      padding: 5px 10px;
      border: none;
      background-color: #fff;
      cursor: pointer;
      border-radius: 3px;
      font-size: 14px;
    }
    .menubar button:hover {
      background-color: #eee;
    }
    .menubar button:disabled {
      background-color: #ccc;
      cursor: not-allowed;
    }  `, ]
      ;

 async _isItemActive(item) {
    if (this.editorView && typeof item.command === 'function') {
      return item.command(this.editorView.state, null, this.editorView);
    }
    return false;
  }

  async _onItemClick(event, item) {
    event.preventDefault();
    if (this.editorView) {
      this.editorView.focus();
      item.command(this.editorView.state, this.editorView.dispatch, this.editorView);
    }
  }

async _updateMenu() {
    if (this.items && this.editorView) {
      this.items.forEach((item) => {
        const isActive = this._isItemActive(item);
        item.dom && (item.dom.style.display = isActive ? '' : 'none');
      });
    }
  }

connectedCallback(): void {
    super.connectedCallback();
    console.log("editor menu component added");
    this._updateMenu();


}

disconnectedCallback(): void {
    super.disconnectedCallback();
    this.items.forEach((item) => {
      if (item.dom) {
        item.dom.remove();
      }
    });

}


render() {

    return html`

    <app-header></app-header>

    <main>

    </main>

    `
}
}

customElements.define('menu-bar', MenuBar)