import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useData, useInput } from "../../data/index.js";
import { getDurableEvent, listDurableEvents } from "../../../../data/index.js";
import { dispatchEvent } from "../../../../events/index.js";
import { path as createPath } from "./schedule.js";
export const path = "/durable-events";
export const anonymous = true;
export const cached = true;
const LINK_CLASS = "text-blue-600 hover:bg-white underline hover:underline-offset-2";
export async function handler() {
    return {
        events: await listDurableEvents({
            type: "dispatch"
        }),
    };
}
export async function submit(request) {
    if (request.body?.dispatch) {
        const event = await getDurableEvent({
            type: "dispatch",
            durableEventId: request.body.dispatch
        });
        if (event) {
            await dispatchEvent(event);
        }
    }
}
export function ListDurableEvents() {
    const { events } = useInput();
    const { isUnauthenticated } = useData();
    return (_jsxs("div", { className: "flex flex-col", children: [!isUnauthenticated ? _jsx("a", { href: createPath, className: LINK_CLASS, children: "Schedule Event" }) : undefined, _jsx("div", { className: "flex flex-col divide-y", children: events.map(event => (_jsxs("div", { className: "flex flex-row justify-between", children: [_jsx("div", { children: Array.isArray(event.dispatch) ? "Multiple events" : event.dispatch.type }), !isUnauthenticated ? (_jsxs("form", { action: path, method: "POST", children: [_jsx("input", { type: "hidden", name: "dispatch", value: event.durableEventId }), _jsx("button", { type: "submit", className: "bg-sky-500 hover:bg-sky-700 px-4 py-2.5 text-sm leading-5 rounded-md font-semibold text-white", children: "Dispatch" })] })) : undefined] }, event.durableEventId))) })] }));
}
export const Component = ListDurableEvents;
//# sourceMappingURL=list.js.map