import { html, TemplateResult } from 'lit';
import { UiDemoPage } from '../ui/UiDemoPage.js';
import { ContextualMenu } from '../../../src/contextual-menu/ContextualMenu.js';
import { IContextMenuCommand, IEnabledOptions, IExecuteOptions } from '../../../src/contextual-menu/types.js';
import { copy, paste } from './DemoIcons.js';

class ComponentPage extends UiDemoPage {
  componentName = 'Enabled state demo';

  firstRender(): void {
    super.firstRender();
    this.initContextMenu();
  }

  initContextMenu(): void {
    const container = document.body.querySelector('#workspace') as HTMLElement;
    const instance = new ContextualMenu(container);
    const commands: IContextMenuCommand[] = ([
      {
        target: 'root',
        label: 'Paste',
        title: 'Paste the value',
        icon: paste,
        execute: (args: IExecuteOptions): void => {
          console.log(args);
          const copyId = args.store.get('copy');
          console.log('Pasting', copyId);
          args.store.delete('copy');
        },
        enabled: (args: IEnabledOptions): boolean => {
          const copyId = args.store.get('copy');
          return !!copyId;
        },
      },
      {
        target: 'div.target',
        label: 'Copy',
        title: 'Copies the object',
        icon: copy,
        execute: (args: IExecuteOptions): void => {
          const { id } = args.target.dataset;
          args.store.set('copy', id);
        }
      },
    ]);
    instance.registerCommands(commands);
    instance.connect();
  }

  contentTemplate(): TemplateResult {
    return html`
      <a href="./">Back</a>
      <section class="demo-section">
        <h2 class="title-large">Basic contextual menu</h2>
        <div id="workspace">
          <div class="target" data-id="test-value"></div>
        </div>
        <p>The "paste" command is enabled after the "copy" command on the target box is executed.</p>
      </section>
    `;
  }
}

const instance = new ComponentPage();
instance.render();
