/** @odoo-module **/

import {Dropdown} from "@web/core/dropdown/dropdown";
import {DropdownItem} from "@web/core/dropdown/dropdown_item";
import {TranslateMenuBasic} from "@translation_helper/translate/translate_menu_basic";
import {_t} from "@web/core/l10n/translation";
import {useCommand} from "@web/core/commands/command_hook";
import {useEnvTranslateContext} from "./translate_context";
import {useService} from "@web/core/utils/hooks";

export class TranslateMenu extends TranslateMenuBasic {
    setup() {
        super.setup();
        const translateContext = useEnvTranslateContext();
        this.command = useService("command");
        useCommand(
            _t("Translate tools..."),
            async () => {
                const items = await translateContext.getItems(this.env);
                let index = 0;
                const defaultCategories = items.filter((item) => item.type === "separator").map(() => (index += 1));
                const provider = {
                    async provide() {
                        const categories = [...defaultCategories];
                        let category = categories.shift();
                        const result = [];
                        items.forEach((item) => {
                            if (item.type === "item") {
                                result.push({
                                    name: item.description.toString(),
                                    action: item.callback,
                                    category,
                                });
                            } else if (item.type === "separator") {
                                category = categories.shift();
                            }
                        });
                        return result;
                    },
                };
                const configByNamespace = {
                    default: {
                        categories: defaultCategories,
                        emptyMessage: _t("No translate command found"),
                        placeholder: _t("Choose a translate command..."),
                    },
                };
                const commandPaletteConfig = {
                    configByNamespace,
                    providers: [provider],
                };
                return commandPaletteConfig;
            },
            {
                category: "translate",
            }
        );
    }
}
TranslateMenu.components = {Dropdown, DropdownItem};
TranslateMenu.props = {};
