(function () {
    const config = window.gfMigrationNotice || {};
    const strings = config.strings || {};

    function apiPath(path) {
        const base = String(config.restBase || "").replace(/\/$/, "");
        return base.replace(/^.*\/wp-json/, "") + path;
    }

    function dismissNotice(button) {
        const slug = button.dataset.gfMigrationDismiss || "";
        const notice = button.closest("[data-gf-migration-notice]");
        if (!slug || !notice || !window.wp || !window.wp.apiFetch) {
            return;
        }

        button.disabled = true;

        window.wp.apiFetch({
            path: apiPath("/migration/notice-dismiss"),
            method: "POST",
            headers: {
                "X-WP-Nonce": config.restNonce || ""
            },
            data: {
                slug: slug
            }
        }).then(function () {
            notice.classList.add("is-dismissing");
            window.setTimeout(function () {
                notice.remove();
            }, 200);
        }).catch(function () {
            button.disabled = false;
            window.alert(strings.dismissError || "Migration notice dismissal failed.");
        });
    }

    document.querySelectorAll("[data-gf-migration-dismiss]").forEach(function (button) {
        button.addEventListener("click", function () {
            dismissNotice(button);
        });
    });
}());
