/** @odoo-module */

import {Component} from "@odoo/owl";
import {Dialog} from "@web/core/dialog/dialog";
import {_t} from "@web/core/l10n/translation";
import {editModelTranslate} from "@translation_helper/translate/translate_utils";
import {registry} from "@web/core/registry";

const translateRegistry = registry.category("translate");

function viewSeparator() {
    return {type: "separator", sequence: 300};
}

// ------------------------------------------------------------------------------
// Get view
// ------------------------------------------------------------------------------

class GetViewDialog extends Component {
    setup() {
        this.title = _t("Get View");
    }
}
GetViewDialog.template = "web.TranslateMenu.GetViewDialog";
GetViewDialog.components = {Dialog};
GetViewDialog.props = {
    arch: {type: Element},
    close: {type: Function},
};

// // ------------------------------------------------------------------------------
// // Edit View
// // ------------------------------------------------------------------------------

export function editView({accessRights, component, env}) {
    if (!accessRights.canEditView) {
        return null;
    }
    const {viewId, viewType: type} = component.env.config || {};
    if (!type) {
        return;
    }
    const displayName = type[0].toUpperCase() + type.slice(1);
    const description = _t("Translate View: ") + displayName;
    return {
        type: "item",
        description,
        callback: () => {
            editModelTranslate(env, description, "ir.ui.view", viewId, "soft_reload");
        },
        sequence: 350,
    };
}

translateRegistry.category("view").add("editView", editView);

translateRegistry.category("view").add("viewSeparator", viewSeparator);

// ------------------------------------------------------------------------------
// Edit SearchView
// ------------------------------------------------------------------------------

export function editSearchView({accessRights, component, env}) {
    if (!accessRights.canEditView) {
        return null;
    }
    const {searchViewId} = component.props.info || {};
    if (searchViewId === undefined) {
        return null;
    }
    const description = _t("Translate SearchView");
    return {
        type: "item",
        description,
        callback: () => {
            editModelTranslate(env, description, "ir.ui.view", searchViewId, "reload");
        },
        sequence: 350,
    };
}

translateRegistry.category("view").add("editSearchView", editSearchView);
