import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { addInviteeState, } from "../../../../data/index.js";
import { ok } from "../../../../is.js";
import { isRole, getAuthenticationRoles } from "../../../../authentication/index.js";
import { useError, useMaybeBody, useMaybeResult } from "../../data/index.js";
export const path = "/invite/create";
const { INVITE_ANONYMOUS, INVITE_REPEATING, INVITE_AUTO_ACCEPT } = process.env;
const FORM_CLASS = `
mt-1
block
w-full
md:max-w-sm
rounded-md
border-gray-300
shadow-sm
focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50
disabled:bg-slate-300 disabled:cursor-not-allowed
`.trim();
const FORM_GROUP_CLASS = `block py-2`;
export async function submit(request) {
    const { role, anonymous, repeating, autoAccept, redirectUrl } = request.body;
    console.log(role, getAuthenticationRoles());
    ok(isRole(role), "Must be the role to invite as role");
    let data;
    if (request.body.data) {
        try {
            data = JSON.parse(request.body.data);
        }
        catch { }
    }
    const { inviteUrl } = await addInviteeState({
        roles: [role],
        inviteAnonymous: anonymous,
        inviteRepeating: repeating,
        inviteAutoAccept: autoAccept,
        inviteRedirectUrl: redirectUrl,
        data
    });
    return { inviteUrl, role };
}
export function CreateInvite() {
    const body = useMaybeBody();
    const result = useMaybeResult();
    const error = useError();
    console.error(error);
    return _jsx(Body, { body: result ? undefined : body });
    function Body({ body }) {
        return (_jsxs("form", { name: "create-invite", action: `${path}#action-section`, method: "post", children: [_jsx("div", { className: "flex flex-col", children: _jsxs("label", { className: FORM_GROUP_CLASS, children: [_jsx("span", { className: "text-gray-700", children: "What role would you like to invite?" }), _jsx("select", { name: "role", className: FORM_CLASS, defaultValue: body?.role ?? getAuthenticationRoles()[0], children: getAuthenticationRoles().map((role, index) => _jsx("option", { value: role, children: role }, index)) })] }) }), _jsx("div", { className: "flex flex-col", children: _jsxs("label", { className: FORM_GROUP_CLASS, children: [_jsx("span", { className: "text-gray-700", children: "Redirect to URL after accepting?" }), _jsx("input", { type: "text", name: "redirectUrl", className: FORM_CLASS, defaultValue: body?.redirectUrl ?? "" })] }) }), INVITE_ANONYMOUS ? (_jsxs("div", { className: "flex items-center mb-4", children: [_jsx("input", { id: "anonymous", name: "anonymous", defaultChecked: body?.anonymous, type: "checkbox", className: "h-4 w-4 rounded border-gray-300 text-indigo-600 focus:ring-indigo-500" }), _jsx("div", { className: "ml-2", children: _jsx("label", { htmlFor: "save-as-user-default", className: "text-sm font-medium text-gray-900", children: "Allow anonymous user" }) })] })) : undefined, INVITE_REPEATING ? (_jsxs("div", { className: "flex items-center mb-4", children: [_jsx("input", { id: "repeating", name: "repeating", defaultChecked: body?.repeating, type: "checkbox", className: "h-4 w-4 rounded border-gray-300 text-indigo-600 focus:ring-indigo-500" }), _jsx("div", { className: "ml-2", children: _jsx("label", { htmlFor: "save-as-user-default", className: "text-sm font-medium text-gray-900", children: "Allow repeated link use" }) })] })) : undefined, INVITE_AUTO_ACCEPT ? (_jsxs("div", { className: "flex items-center mb-4", children: [_jsx("input", { id: "autoAccept", name: "autoAccept", defaultChecked: body?.autoAccept, type: "checkbox", className: "h-4 w-4 rounded border-gray-300 text-indigo-600 focus:ring-indigo-500" }), _jsx("div", { className: "ml-2", children: _jsx("label", { htmlFor: "save-as-user-default", className: "text-sm font-medium text-gray-900", children: "Auto accept invite" }) })] })) : undefined, _jsx("div", { className: "flex flex-col", children: _jsxs("label", { className: FORM_GROUP_CLASS, children: [_jsx("span", { className: "text-gray-700", children: "Include JSON data in authentication state?" }), _jsx("textarea", { name: "data", className: FORM_CLASS, defaultValue: body?.data ?? "" })] }) }), _jsx("div", { id: "action-section", children: _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: "Create Invite" }) }), result ? (_jsxs("div", { children: [_jsx("br", {}), _jsx("br", {}), _jsxs("p", { children: ["Created invite url for the role \"", result.role, "\":"] }), _jsx("br", {}), _jsx("pre", { children: result.inviteUrl })] })) : undefined] }));
    }
}
export const Component = CreateInvite;
//# sourceMappingURL=create.js.map