import wn from '../../../../../node_modules/preact/compat/dist/compat.module.mjs';
import '../../../../../_virtual/index2.mjs';
import { useSWRConfig } from '../../../../../node_modules/swr/_internal/dist/index.mjs';
import { Button } from '../components/Button.mjs';
import { FlexBox } from '../shared/components/FlexBox.mjs';
import { Input } from '../components/Input.mjs';
import { Select } from '../components/Select.mjs';
import { Typography } from '../components/Typography.mjs';
import { organizationGetKey } from '../utils/organizationGetKey.mjs';
import { useStytchClient } from '../utils/useStytchClient.mjs';
import { scimIdpMap } from './scimInfo.mjs';
import { useScimRouterController } from './SCIMRouter.mjs';
import { useState as h, useCallback as T } from '../../../../../node_modules/preact/hooks/dist/hooks.module.mjs';

const idpSelectItems$1 = Object.entries(scimIdpMap).map(([idp, { displayName }])=>({
        value: idp,
        label: displayName
    }));
const SCIMNewConnectionScreen = ()=>{
    const { navigate } = useScimRouterController();
    const [idp, setIdp] = h();
    const [displayName, setDisplayName] = h('');
    const canCreate = displayName && idp;
    const client = useStytchClient();
    const { mutate } = useSWRConfig();
    const handleSubmit = T(async (e)=>{
        e.preventDefault();
        if (!canCreate) {
            return;
        }
        const response = await mutate('scim_create', async ()=>{
            const params = {
                display_name: displayName,
                identity_provider: idp
            };
            const result = await client.scim.createConnection(params);
            const connection = result.connection;
            mutate(organizationGetKey(connection.organization_id), async (org)=>{
                if (org) {
                    return {
                        ...org,
                        scim_active_connection: {
                            connection_id: connection.connection_id,
                            display_name: connection.display_name
                        }
                    };
                }
            });
            return connection;
        });
        if (response) {
            navigate({
                screen: 'newConnectionConfigure',
                params: {
                    connection: response
                }
            });
        }
    }, [
        canCreate,
        client.scim,
        displayName,
        idp,
        mutate,
        navigate
    ]);
    return /*#__PURE__*/ wn.createElement("form", {
        onSubmit: handleSubmit
    }, /*#__PURE__*/ wn.createElement(FlexBox, {
        flexDirection: "column",
        gap: 3
    }, /*#__PURE__*/ wn.createElement(Typography, {
        variant: "h2"
    }, "Create SCIM Connection"), /*#__PURE__*/ wn.createElement(FlexBox, {
        flexDirection: "column"
    }, /*#__PURE__*/ wn.createElement(Typography, null, "Select your IdP."), /*#__PURE__*/ wn.createElement(Select, {
        width: 360,
        placeholder: "Select",
        value: idp,
        selectItems: idpSelectItems$1,
        onChange: (value)=>setIdp(value)
    })), /*#__PURE__*/ wn.createElement(FlexBox, {
        flexDirection: "column"
    }, /*#__PURE__*/ wn.createElement(Typography, null, "Enter a display name for your SCIM connection."), /*#__PURE__*/ wn.createElement(Input, {
        value: displayName,
        onChange: (value)=>setDisplayName(value),
        placeholder: "Display name"
    })), /*#__PURE__*/ wn.createElement(Typography, null, "Once you click “Create” the SCIM connection will be created. If you need to change your IdP after this, you will need to delete the connection and recreate."), /*#__PURE__*/ wn.createElement(FlexBox, {
        flexDirection: "row",
        gap: 2
    }, /*#__PURE__*/ wn.createElement(Button, {
        variant: "ghost",
        onClick: ()=>{
            navigate({
                screen: 'scimConnection'
            });
        }
    }, "Cancel"), /*#__PURE__*/ wn.createElement(Button, {
        type: "submit",
        disabled: !canCreate
    }, "Create"))));
};

export { SCIMNewConnectionScreen };
//# sourceMappingURL=SCIMNewConnectionScreen.mjs.map
