import {AiEditorOptions, AiEditorEventListener} from "../core/AiEditor.ts";
import {EditorEvents} from "@tiptap/core";
import {Undo} from "./menus/Undo";
import {AbstractMenuButton} from "./AbstractMenuButton.ts";
import {Redo} from "./menus/Redo";
import {Heading} from "./menus/Heading.ts";
import {FontFamily} from "./menus/FontFamily";
import {FontSize} from "./menus/FontSize";
import {Bold} from "./menus/Bold";
import {Italic} from "./menus/Italic";
import {Underline} from "./menus/Underline";
import {Strike} from "./menus/Strike";
import {Subscript} from "./menus/Subscript";
import {Superscript} from "./menus/Superscript";
import {Highlight} from "./menus/Highlight";
import {FontColor} from "./menus/FontColor";
import {Divider} from "./menus/Divider";
import {BulletList} from "./menus/BulletList";
import {OrderedList} from "./menus/OrderedList";
import {IndentDecrease} from "./menus/IndentDecrease";
import {IndentIncrease} from "./menus/IndentIncrease";
import {Align} from "./menus/Align";
import {Link} from "./menus/Link";
import {SourceCode} from "./menus/SourceCode";
import {Todo} from "./menus/Todo";
import {LineHeight} from "./menus/LineHeight";
import {Quote} from "./menus/Quote";
import {Image} from "./menus/Image";
import {Video} from "./menus/Video";
import {Code} from "./menus/Code";
import {CodeBlock} from "./menus/CodeBlock";
import {Eraser} from "./menus/Eraser";
import {Hr} from "./menus/Hr";
import {Table} from "./menus/Table";
import {Break} from "./menus/Break";
import {Attachment} from "./menus/Attachment";
import {Fullscreen} from "./menus/Fullscreen";
import {Printer} from "./menus/Printer";
import {Emoji} from "./menus/Emoji";
import {Painter} from "./menus/Painter";
import {Ai} from "./menus/Ai.ts";
import {Container} from "./menus/Container.ts";
import {Custom} from "./menus/Custom.ts";
import {defineCustomElement} from "../commons/defineCustomElement.ts";
import {Group} from "./menus/Group.ts";
import {initToolbarKeys} from "../util/initToolbarKeys.ts";
import { defaultToolbarKeys } from "./DefaultToolbarKeys.ts";

defineCustomElement('linwiteditor-undo', Undo);
defineCustomElement('linwiteditor-redo', Redo);
defineCustomElement('linwiteditor-brush', Painter);
defineCustomElement('linwiteditor-container', Container);
defineCustomElement('linwiteditor-custom', Custom);
defineCustomElement('linwiteditor-eraser', Eraser);
defineCustomElement('linwiteditor-heading', Heading);
defineCustomElement('linwiteditor-font-family', FontFamily);
defineCustomElement('linwiteditor-font-size', FontSize);
defineCustomElement('linwiteditor-bold', Bold);
defineCustomElement('linwiteditor-italic', Italic);
defineCustomElement('linwiteditor-underline', Underline);
defineCustomElement('linwiteditor-strike', Strike);
defineCustomElement('linwiteditor-link', Link);
defineCustomElement('linwiteditor-source-code', SourceCode);
defineCustomElement('linwiteditor-code', Code);
defineCustomElement('linwiteditor-subscript', Subscript);
defineCustomElement('linwiteditor-superscript', Superscript);
defineCustomElement('linwiteditor-highlight', Highlight);
defineCustomElement('linwiteditor-font-color', FontColor);
defineCustomElement('linwiteditor-divider', Divider);
defineCustomElement('linwiteditor-bullet-list', BulletList);
defineCustomElement('linwiteditor-ordered-list', OrderedList);
defineCustomElement('linwiteditor-indent-decrease', IndentDecrease);
defineCustomElement('linwiteditor-indent-increase', IndentIncrease);
defineCustomElement('linwiteditor-align', Align);
defineCustomElement('linwiteditor-todo', Todo);
defineCustomElement('linwiteditor-line-height', LineHeight);
defineCustomElement('linwiteditor-break', Break);
defineCustomElement('linwiteditor-quote', Quote);
defineCustomElement('linwiteditor-image', Image);
defineCustomElement('linwiteditor-video', Video);
defineCustomElement('linwiteditor-code-block', CodeBlock);
defineCustomElement('linwiteditor-hr', Hr);
defineCustomElement('linwiteditor-table', Table);
defineCustomElement('linwiteditor-attachment', Attachment);
defineCustomElement('linwiteditor-fullscreen', Fullscreen);
defineCustomElement('linwiteditor-printer', Printer);
defineCustomElement('linwiteditor-emoji', Emoji);
defineCustomElement('linwiteditor-ai', Ai);
defineCustomElement('linwiteditor-group', Group);

export type MenuButtonOptions = {
    key: string,
    title: string,
    svg: string,
}

export class Header extends HTMLElement implements AiEditorEventListener {
    // template:string;
    menuButtons: AbstractMenuButton[] = [];

    constructor() {
        super();
    }

    connectedCallback() {
        if (this.children && this.children.length > 0) {
            return
        }
        const divElement = document.createElement("div");
        for (let menuButton of this.menuButtons) {
            divElement.appendChild(menuButton);
        }
        divElement.style.display = "flex"
        divElement.style.flexWrap = "wrap"
        this.appendChild(divElement)
    }

    onCreate(event: EditorEvents["create"], options: AiEditorOptions): void {
        let toolbarKeys = options.toolbarKeys || defaultToolbarKeys;
        toolbarKeys = toolbarKeys.filter((tool) => {
            if (typeof tool === "string") {
                return !options.toolbarExcludeKeys?.includes(tool as any);
            }
            return true;
        }).filter((tool, index, array) => {
            const prevTool = array[index - 1];
            if (typeof tool === "string" && (typeof prevTool === "string" || typeof prevTool === "undefined")) {
                const dividers = ["divider", "|", undefined];
                return dividers.includes(tool) ? !dividers.includes(prevTool) : true;
            }
            return true;
        })
        initToolbarKeys(event, options, this.menuButtons, toolbarKeys);
    }


    onTransaction(event: EditorEvents["transaction"]): void {
        for (let menuButton of this.menuButtons) {
            menuButton.onTransaction(event);
        }
    }

    onEditableChange(editable: boolean) {
        for (let menuButton of this.menuButtons) {
            menuButton.onEditableChange(editable);
        }
    }


}



