import Draggabilly, { type MoveVector } from "draggabilly";
import { t } from "../services/i18n.js";
import BasicWidget from "./basic_widget.js";
import contextMenu from "../menus/context_menu.js";
import utils from "../services/utils.js";
import keyboardActionService from "../services/keyboard_actions.js";
import appContext, { type CommandNames, type CommandListenerData, type EventData } from "../components/app_context.js";
import froca from "../services/froca.js";
import attributeService from "../services/attributes.js";
import type NoteContext from "../components/note_context.js";
const isDesktop = utils.isDesktop();
const TAB_CONTAINER_MIN_WIDTH = 100;
const TAB_CONTAINER_MAX_WIDTH = 240;
const TAB_CONTAINER_LEFT_PADDING = 5;
const SCROLL_BUTTON_WIDTH = 36;
const NEW_TAB_WIDTH = 36;
const MIN_FILLER_WIDTH = isDesktop ? 50 : 15;
const MARGIN_WIDTH = 5;
const TAB_SIZE_SMALL = 84;
const TAB_SIZE_SMALLER = 60;
const TAB_SIZE_MINI = 48;
const TAB_TPL = `
`;
const CONTAINER_ANCHOR_TPL = ``;
const NEW_TAB_BUTTON_TPL = `+
`;
const FILLER_TPL = ``;
const TAB_ROW_TPL = `
`;
export default class TabRowWidget extends BasicWidget {
    private isDragging?: boolean;
    private showNoteIcons?: boolean;
    private draggabillies!: Draggabilly[];
    private draggabillyDragging?: Draggabilly | null;
    private $style!: JQuery;
    private $tabScrollingContainer!: JQuery;
    private $tabContainer!: JQuery;
    private $scrollButtonLeft!: JQuery;
    private $scrollButtonRight!: JQuery;
    private $containerAnchor!: JQuery;
    private $filler!: JQuery;
    private $newTab!: JQuery;
    private updateScrollTimeout: ReturnType | undefined;
    private newTabOuterWidth: number = 0;
    private scrollButtonsOuterWidth: number = 0;
    doRender() {
        this.$widget = $(TAB_ROW_TPL);
        this.$tabScrollingContainer = this.$widget.children(".tab-row-widget-scrolling-container");
        this.$tabContainer = this.$widget.find(".tab-row-widget-container");
        this.$scrollButtonLeft = this.$widget.children(".tab-scroll-button-left");
        this.$scrollButtonRight = this.$widget.children(".tab-scroll-button-right");
        const documentStyle = window.getComputedStyle(document.documentElement);
        this.showNoteIcons = documentStyle.getPropertyValue("--tab-note-icons") === "true";
        this.draggabillies = [];
        this.setupStyle();
        this.setupEvents();
        this.setupContainerAnchor();
        this.setupDraggabilly();
        this.setupNewButton();
        this.setupFiller();
        this.layoutTabs();
        this.setVisibility();
        this.setupScrollEvents();
        this.$widget.on("contextmenu", ".note-tab", (e) => {
            e.preventDefault();
            const ntxId = $(e.target).closest(".note-tab").attr("data-ntx-id");
            contextMenu.show({
                x: e.pageX,
                y: e.pageY,
                items: [
                    { title: t("tab_row.close"), command: "closeTab", uiIcon: "bx bx-x" },
                    { title: t("tab_row.close_other_tabs"), command: "closeOtherTabs", uiIcon: "bx bx-empty", enabled: appContext.tabManager.noteContexts.length !== 1 },
                    { title: t("tab_row.close_right_tabs"), command: "closeRightTabs", uiIcon: "bx bx-empty", enabled: appContext.tabManager.noteContexts?.at(-1)?.ntxId !== ntxId },
                    { title: t("tab_row.close_all_tabs"), command: "closeAllTabs", uiIcon: "bx bx-empty" },
                    { title: "----" },
                    { title: t("tab_row.reopen_last_tab"), command: "reopenLastTab", uiIcon: "bx bx-undo", enabled: appContext.tabManager.recentlyClosedTabs.length !== 0 },
                    { title: "----" },
                    { title: t("tab_row.move_tab_to_new_window"), command: "moveTabToNewWindow", uiIcon: "bx bx-window-open" },
                    { title: t("tab_row.copy_tab_to_new_window"), command: "copyTabToNewWindow", uiIcon: "bx bx-empty" }
                ],
                selectMenuItemHandler: ({ command }) => {
                    if (command) {
                        this.triggerCommand(command, { ntxId });
                    }
                }
            });
        });
    }
    setupStyle() {
        this.$style = $("