/**
 * QUnit Tests for Tooltip and Label Functions
 * Tests tooltip initialization and label loading
 */

odoo.define("website_sale_aplicoop.test_tooltips_labels", function (require) {
    "use strict";

    var QUnit = window.QUnit;

    QUnit.module(
        "website_sale_aplicoop.tooltips_labels",
        {
            beforeEach: function () {
                // Setup: Create test DOM elements
                this.$fixture = $("#qunit-fixture");

                // Add test buttons with tooltip labels
                this.$fixture.append(
                    '<button id="test-btn-1" data-tooltip-label="save_cart">Save</button>' +
                        '<button id="test-btn-2" data-tooltip-label="checkout">Checkout</button>' +
                        '<button id="test-btn-3" data-tooltip-label="reload_cart">Reload</button>'
                );

                // Initialize groupOrderShop
                window.groupOrderShop = {
                    orderId: "1",
                    cart: {},
                    labels: {
                        save_cart: "Guardar Carrito",
                        reload_cart: "Recargar Carrito",
                        checkout: "Proceder al Pago",
                        confirm_order: "Confirmar Pedido",
                        back_to_cart: "Volver al Carrito",
                    },
                    _initTooltips: function () {
                        var labels = window.groupOrderShop.labels || this.labels || {};
                        var tooltipElements = document.querySelectorAll("[data-tooltip-label]");

                        tooltipElements.forEach(function (el) {
                            var labelKey = el.getAttribute("data-tooltip-label");
                            if (labelKey && labels[labelKey]) {
                                el.setAttribute("title", labels[labelKey]);
                            }
                        });
                    },
                };
            },
            afterEach: function () {
                // Cleanup
                this.$fixture.empty();
                delete window.groupOrderShop;
            },
        },
        function () {
            QUnit.test("tooltips are initialized from labels", function (assert) {
                assert.expect(3);

                // Initialize tooltips
                window.groupOrderShop._initTooltips();

                var btn1 = document.getElementById("test-btn-1");
                var btn2 = document.getElementById("test-btn-2");
                var btn3 = document.getElementById("test-btn-3");

                assert.equal(
                    btn1.getAttribute("title"),
                    "Guardar Carrito",
                    "save_cart tooltip is correct"
                );
                assert.equal(
                    btn2.getAttribute("title"),
                    "Proceder al Pago",
                    "checkout tooltip is correct"
                );
                assert.equal(
                    btn3.getAttribute("title"),
                    "Recargar Carrito",
                    "reload_cart tooltip is correct"
                );
            });

            QUnit.test("tooltips handle missing labels gracefully", function (assert) {
                assert.expect(1);

                // Add button with non-existent label
                this.$fixture.append(
                    '<button id="test-btn-4" data-tooltip-label="non_existent">Test</button>'
                );

                window.groupOrderShop._initTooltips();

                var btn4 = document.getElementById("test-btn-4");
                var title = btn4.getAttribute("title");

                // Should be null or empty since label doesn't exist
                assert.ok(!title || title === "", "missing label does not set tooltip");
            });

            QUnit.test("labels object contains expected keys", function (assert) {
                assert.expect(5);

                var labels = window.groupOrderShop.labels;

                assert.ok("save_cart" in labels, "has save_cart label");
                assert.ok("reload_cart" in labels, "has reload_cart label");
                assert.ok("checkout" in labels, "has checkout label");
                assert.ok("confirm_order" in labels, "has confirm_order label");
                assert.ok("back_to_cart" in labels, "has back_to_cart label");
            });

            QUnit.test("labels are strings", function (assert) {
                assert.expect(5);

                var labels = window.groupOrderShop.labels;

                assert.equal(typeof labels.save_cart, "string", "save_cart is string");
                assert.equal(typeof labels.reload_cart, "string", "reload_cart is string");
                assert.equal(typeof labels.checkout, "string", "checkout is string");
                assert.equal(typeof labels.confirm_order, "string", "confirm_order is string");
                assert.equal(typeof labels.back_to_cart, "string", "back_to_cart is string");
            });

            QUnit.test("_initTooltips uses window.groupOrderShop.labels", function (assert) {
                assert.expect(1);

                // Update global labels
                window.groupOrderShop.labels = {
                    save_cart: "Updated Label",
                    checkout: "Updated Checkout",
                    reload_cart: "Updated Reload",
                };

                window.groupOrderShop._initTooltips();

                var btn1 = document.getElementById("test-btn-1");
                assert.equal(
                    btn1.getAttribute("title"),
                    "Updated Label",
                    "uses updated global labels"
                );
            });

            QUnit.test("tooltips can be reinitialized", function (assert) {
                assert.expect(2);

                // First initialization
                window.groupOrderShop._initTooltips();
                var btn1 = document.getElementById("test-btn-1");
                assert.equal(btn1.getAttribute("title"), "Guardar Carrito", "first init correct");

                // Update labels and reinitialize
                window.groupOrderShop.labels.save_cart = "New Translation";
                window.groupOrderShop._initTooltips();

                assert.equal(
                    btn1.getAttribute("title"),
                    "New Translation",
                    "reinitialized with new label"
                );
            });

            QUnit.test("elements without data-tooltip-label are ignored", function (assert) {
                assert.expect(1);

                this.$fixture.append('<button id="test-btn-no-label">No Label</button>');

                window.groupOrderShop._initTooltips();

                var btnNoLabel = document.getElementById("test-btn-no-label");
                var title = btnNoLabel.getAttribute("title");

                assert.ok(!title || title === "", "button without data-tooltip-label has no title");
            });

            QUnit.test("querySelectorAll finds all tooltip elements", function (assert) {
                assert.expect(1);

                var tooltipElements = document.querySelectorAll("[data-tooltip-label]");

                // We have 3 buttons with data-tooltip-label
                assert.equal(
                    tooltipElements.length,
                    3,
                    "finds all 3 elements with data-tooltip-label"
                );
            });

            QUnit.test("labels survive JSON serialization", function (assert) {
                assert.expect(2);

                var labels = window.groupOrderShop.labels;
                var serialized = JSON.stringify(labels);
                var deserialized = JSON.parse(serialized);

                assert.ok(serialized, "labels can be serialized to JSON");
                assert.deepEqual(deserialized, labels, "deserialized labels match original");
            });

            QUnit.test("empty labels object does not break initialization", function (assert) {
                assert.expect(1);

                window.groupOrderShop.labels = {};

                try {
                    window.groupOrderShop._initTooltips();
                    assert.ok(true, "initialization with empty labels does not throw error");
                } catch (e) {
                    assert.ok(false, "initialization threw error: " + e.message);
                }
            });
        }
    );

    return {};
});
