import { jsx as _jsx } from "react/jsx-runtime";
import { AddDriveModal as ConnectAddLocalDriveModal, toast, } from "@powerhousedao/design-system";
import { addDrive, addRemoteDrive, closePHModal, setSelectedDrive, useDriveEditorModules, usePHModal, } from "@powerhousedao/reactor-browser";
import { useConnectCrypto, useUser, } from "@powerhousedao/reactor-browser/connect";
import { requestPublicDrive } from "document-drive";
import { t } from "i18next";
export function AddDriveModal() {
    const phModal = usePHModal();
    const open = phModal?.type === "addDrive";
    const user = useUser();
    const connectCrypto = useConnectCrypto();
    const driveEditorModules = useDriveEditorModules();
    const onAddLocalDrive = async (data) => {
        try {
            const app = driveEditorModules?.find((a) => a.id === data.appId);
            const newDrive = await addDrive({
                id: "",
                slug: "",
                global: {
                    name: data.name,
                    icon: null,
                },
                local: {
                    availableOffline: data.availableOffline,
                    sharingType: data.sharingType.toLowerCase(),
                    listeners: [],
                    triggers: [],
                },
            }, app?.id);
            toast(t("notifications.addDriveSuccess"), {
                type: "connect-success",
            });
            if (!newDrive) {
                return;
            }
            setSelectedDrive(newDrive);
        }
        catch (e) {
            console.error(e);
        }
    };
    const onAddRemoteDrive = async (data) => {
        try {
            const newDrive = await addRemoteDrive(data.url, {
                sharingType: data.sharingType,
                availableOffline: data.availableOffline,
                listeners: [
                    {
                        block: true,
                        callInfo: {
                            data: data.url,
                            name: "switchboard-push",
                            transmitterType: "SwitchboardPush",
                        },
                        filter: {
                            branch: ["main"],
                            documentId: ["*"],
                            documentType: ["*"],
                            scope: ["global"],
                        },
                        label: "Switchboard Sync",
                        listenerId: "1",
                        system: true,
                    },
                ],
                triggers: [],
            });
            toast(t("notifications.addDriveSuccess"), {
                type: "connect-success",
            });
            if (!newDrive) {
                return;
            }
            setSelectedDrive(newDrive);
        }
        catch (e) {
            console.error(e);
        }
    };
    async function onAddLocalDriveSubmit(data) {
        await onAddLocalDrive(data);
        closePHModal();
    }
    async function onAddRemoteDriveSubmit(data) {
        await onAddRemoteDrive(data);
        closePHModal();
    }
    const ready = !!driveEditorModules?.length;
    return (_jsx(ConnectAddLocalDriveModal, { open: open && ready, onAddLocalDrive: onAddLocalDriveSubmit, onAddRemoteDrive: onAddRemoteDriveSubmit, requestPublicDrive: async (url) => {
            try {
                const authToken = await connectCrypto?.getBearerToken?.(url, user?.address, true, { expiresIn: 10 });
                return requestPublicDrive(url, {
                    Authorization: `Bearer ${authToken}`,
                });
            }
            catch (error) {
                console.error(error);
                const authToken = await connectCrypto?.getBearerToken?.(url, user?.address, true, { expiresIn: 10 });
                return requestPublicDrive(url, {
                    Authorization: `Bearer ${authToken}`,
                });
            }
        }, onOpenChange: (status) => {
            if (!status)
                return closePHModal();
        }, appOptions: driveEditorModules?.map((pkg) => ({
            id: pkg.id,
            name: pkg.name,
            driveEditor: pkg.id,
        })) || [] }));
}
//# sourceMappingURL=AddDriveModal.js.map