'use strict';
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { Controller } from "@hotwired/stimulus";
/**
 * Reads the `flashes` cookie (written by FOSHttpCacheBundle's
 * FlashMessageListener during the previous request), fetches a Turbo Stream
 * for each flash, and lets Turbo splice the returned markup into the DOM.
 *
 * Stays dumb on purpose: every flash type (modal, notification, banner) is
 * a fully self-describing Turbo Stream — the stream template declares the
 * action + target. Adding a flash type is template-only; no JS changes.
 *
 * Modal flashes stream into the `<turbo-frame id="modal">` inside the
 * Cms:Modal component; modal.ts has a MutationObserver on that frame that
 * opens the dialog as soon as content appears.
 */
export default class default_1 extends Controller {
    connect() {
        const flashes = this.getCookie("flashes");
        if (!flashes)
            return;
        let data;
        try {
            data = JSON.parse(decodeURIComponent(flashes.replace(/\+/g, " ")));
        }
        catch (e) {
            console.error("Failed to parse flashes cookie", e);
            return;
        }
        this.clearCookie("flashes");
        Object.entries(data).forEach(([type, messages]) => {
            if (!Array.isArray(messages))
                return;
            messages.forEach((message) => this.renderFlash(type, message));
        });
    }
    renderFlash(type, message) {
        return __awaiter(this, void 0, void 0, function* () {
            const url = new URL(this.urlValue, window.location.origin);
            url.searchParams.set("type", type);
            if (typeof message === "object" && message !== null) {
                Object.entries(message).forEach(([k, v]) => {
                    if (v !== null && v !== undefined)
                        url.searchParams.set(k, String(v));
                });
            }
            else if (typeof message === "string") {
                url.searchParams.set("text", message);
            }
            const response = yield fetch(url.toString(), {
                headers: { Accept: "text/vnd.turbo-stream.html" },
            });
            if (!response.ok)
                return;
            const stream = yield response.text();
            // Turbo picks up <turbo-stream> custom elements as they enter the
            // DOM; the template decides action + target.
            document.body.insertAdjacentHTML("beforeend", stream);
        });
    }
    getCookie(name) {
        for (const c of document.cookie.split(";")) {
            const t = c.trim();
            if (t.startsWith(name + "="))
                return t.substring(name.length + 1);
        }
        return false;
    }
    clearCookie(name) {
        document.cookie = `${name}=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;`;
    }
}
default_1.values = { url: String };
