export default function announcementModal() {
    return {
        active: 0,
        count: 0,
        intervalMs: 6000,
        scrollThreshold: 180,
        storageKey: "announcement-modal-dismissed-ids",
        announcementIds: [],
        revealed: false,
        hasRevealed: false,
        interval: null,
        scrollHandler: null,

        init() {
            this.count = Number(this.$el.dataset.announcementCount || 0);
            this.intervalMs = Number(this.$el.dataset.announcementIntervalMs || 6000);
            this.scrollThreshold = Number(this.$el.dataset.announcementScrollThreshold || 180);
            this.announcementIds = (this.$el.dataset.announcementIds || "")
                .split(",")
                .map((value) => value.trim())
                .filter(Boolean);

            if (this.areAllAnnouncementsDismissed()) {
                this.hasRevealed = true;
                return;
            }

            this.scrollHandler = () => this.handleScroll();
            window.addEventListener("scroll", this.scrollHandler, { passive: true });
            this.handleScroll();
        },

        destroy() {
            if (this.scrollHandler) {
                window.removeEventListener("scroll", this.scrollHandler);
                this.scrollHandler = null;
            }

            this.pauseRotation();
        },

        handleScroll() {
            if (this.hasRevealed) {
                return;
            }

            if (window.scrollY >= this.scrollThreshold) {
                this.reveal();
            }
        },

        reveal() {
            if (this.hasRevealed) {
                return;
            }

            this.revealed = true;
            this.hasRevealed = true;
            this.start();
            this.removeScrollHandler();
        },

        close() {
            this.revealed = false;
            this.pauseRotation();
        },

        dismissAnnouncements() {
            if (this.announcementIds.length === 0) {
                this.close();
                return;
            }

            const dismissedIds = new Set(this.getDismissedIds());

            this.announcementIds.forEach((id) => dismissedIds.add(id));
            this.setDismissedIds([...dismissedIds]);
            this.close();
        },

        goTo(index) {
            this.active = index;
        },

        start() {
            if (this.revealed && this.count > 1 && !this.interval) {
                this.interval = window.setInterval(() => {
                    this.active = (this.active + 1) % this.count;
                }, this.intervalMs);
            }
        },

        pauseRotation() {
            if (this.interval) {
                window.clearInterval(this.interval);
                this.interval = null;
            }
        },

        areAllAnnouncementsDismissed() {
            if (this.announcementIds.length === 0) {
                return false;
            }

            const dismissedIds = new Set(this.getDismissedIds());

            return this.announcementIds.every((id) => dismissedIds.has(id));
        },

        getDismissedIds() {
            try {
                const rawValue = window.localStorage.getItem(this.storageKey);
                const ids = rawValue ? JSON.parse(rawValue) : [];

                return Array.isArray(ids) ? ids : [];
            } catch (error) {
                return [];
            }
        },

        setDismissedIds(ids) {
            try {
                window.localStorage.setItem(this.storageKey, JSON.stringify(ids));
            } catch (error) {
            }
        },

        removeScrollHandler() {
            if (this.scrollHandler) {
                window.removeEventListener("scroll", this.scrollHandler);
                this.scrollHandler = null;
            }
        },
    };
}
