import { __awaiter } from "tslib";
import { jsx as _jsx, jsxs as _jsxs } from "@emotion/react/jsx-runtime";
import { Box, Button, VStack, Text, Stack, Alert, AlertIcon, RadioGroup, Radio, FormControl, } from "@chakra-ui/react";
import { useRouter } from "next/router";
import React from "react";
import { useForm } from "react-hook-form";
import qoreStorageClient from "../../hooks/qoreStorageClient";
import "@qorebase/app-runtime/src/integrations/loader";
const PortalAppsConfiguration = () => {
    const router = useRouter();
    const form = useForm({
        mode: "onChange",
    });
    const { data: app, revalidate } = qoreStorageClient
        .table("applications")
        .useGetRow(router.query.appID, { skip: !router.isReady });
    const { status, updateRow: updateApp } = qoreStorageClient
        .table("applications")
        .useUpdateRow();
    const [value, setValue] = React.useState("unlisted");
    React.useEffect(() => {
        if (!app)
            return;
        setValue(app === null || app === void 0 ? void 0 : app.visibility);
    }, [app]);
    return (_jsxs(Box, Object.assign({ width: "full", as: "form", onSubmit: form.handleSubmit(() => __awaiter(void 0, void 0, void 0, function* () {
            yield updateApp(app.id, { visibility: value });
            revalidate();
        })) }, { children: [_jsx(FormControl, { children: _jsx(RadioGroup, Object.assign({ name: "visible", onChange: setValue, value: value }, { children: _jsxs(Stack, { children: [_jsx(Radio, Object.assign({ value: "public", pt: "16px" }, { children: _jsxs(VStack, { children: [_jsx(Text, Object.assign({ w: "100%", fontWeight: "600", fontSize: "md", textAlign: "left" }, { children: "All" })), _jsx(Text, { children: "All roles in the organization can see the applications on the app portal." })] }) })), _jsx(Radio, Object.assign({ value: "unlisted", pt: "16px" }, { children: _jsxs(VStack, { children: [_jsx(Text, Object.assign({ w: "100%", fontWeight: "600", fontSize: "md", textAlign: "left" }, { children: "Internal Team" })), _jsx(Text, { children: "Only roles that are in the team can see the application on the app portal." })] }) }))] }) })) }), _jsx(FormControl, { children: _jsx(Button, Object.assign({ w: "100%", mt: "16px", size: "sm", type: "submit", bg: "#0065FF", color: "white", isLoading: status === "loading", loadingText: "Saving...", disabled: status === "loading" }, { children: "Save" })) })] })));
};
export default function PortalApps() {
    return (_jsx(Box, Object.assign({ pr: "40px" }, { children: _jsxs(VStack, Object.assign({ w: "full" }, { children: [_jsx(Text, Object.assign({ w: "full", align: "left", fontSize: "md", fontWeight: "600", mb: "16px" }, { children: "Apps Portal" })), _jsxs(Alert, Object.assign({ status: "info", borderRadius: "4px", bg: "#E6F0FF" }, { children: [_jsx(AlertIcon, {}), _jsx(Text, Object.assign({ w: "196px" }, { children: "The settings you choose will affect the application that will appear on the app portal." }))] })), _jsx(PortalAppsConfiguration, {})] })) })));
}
//# sourceMappingURL=PortalApps.js.map