import Alpine from "@alpinejs/csp";
import focus from "@alpinejs/focus";
import morph from "@alpinejs/morph";
import stimulusManager from "../js/stimulusManager.js";
import * as Turbo from "@hotwired/turbo";

import "./styles.css";

Alpine.plugin(focus);
Alpine.plugin(morph);

document.addEventListener("submit", (event) => {
    const form = event.target;

    if (!(form instanceof HTMLFormElement) || !form.matches("[data-confirm-submit]")) {
        return;
    }

    const message = form.dataset.confirmMessage || "Confirmer cette action ?";

    if (!window.confirm(message)) {
        event.preventDefault();
    }
});

document.addEventListener("click", (event) => {
    const link = event.target.closest("[data-confirm-link]");

    if (!(link instanceof HTMLAnchorElement)) {
        return;
    }

    const message = link.dataset.confirmMessage || "Confirmer cette action ?";

    if (!window.confirm(message)) {
        event.preventDefault();
    }
});

window.Alpine = Alpine;
window.Turbo = Turbo;

stimulusManager.load(require.context(
    "@symfony/stimulus-bridge/lazy-controller-loader!../controllers",
    true,
    /\.(j|t)sx?$/
));

Alpine.start();
