import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
/*
 * Copyright (c) 2023-2025 Datalayer, Inc.
 * Distributed under the terms of the Modified BSD License.
 */
import { useState, useEffect } from 'react';
import { useParams } from 'react-router-dom';
import { PageHeader, Heading, Text, Button, TextInput, FormControl, Textarea, Label, } from '@primer/react';
import { Box } from '@datalayer/primer-addons';
import { EyeIcon, EyeClosedIcon } from '@primer/octicons-react';
import { BoringAvatar } from '../../components/avatars/index.js';
import { useCache, useToast } from '../../hooks/index.js';
import { useRunStore } from '../../state/index.js';
export const DatasourceDetail = () => {
    const { datasourceId } = useParams();
    const runStore = useRunStore();
    const { enqueueToast } = useToast();
    const { useUpdateDatasource, useDatasource } = useCache();
    const updateDatasourceMutation = useUpdateDatasource();
    const datasourceQuery = useDatasource(datasourceId);
    const [datasource, setDatasource] = useState();
    const [formValues, setFormValues] = useState({
        name: datasource?.name,
        description: datasource?.description,
    });
    const [validationResult, setValidationResult] = useState({
        name: undefined,
        description: undefined,
    });
    const [passwordVisibility, setPasswordVisibility] = useState(false);
    useEffect(() => {
        if (datasourceQuery.data) {
            const datasource = datasourceQuery.data;
            setDatasource(datasource);
            setFormValues({ ...datasource });
        }
    }, [datasourceQuery.data]);
    const nameNameChange = (event) => {
        setFormValues(prevFormValues => ({
            ...prevFormValues,
            name: event.target.value,
        }));
    };
    const nameDescriptionChange = (event) => {
        setFormValues(prevFormValues => ({
            ...prevFormValues,
            description: event.target.value,
        }));
    };
    useEffect(() => {
        setValidationResult({
            ...validationResult,
            name: formValues.name === undefined
                ? undefined
                : formValues.name.length > 2
                    ? true
                    : false,
            description: formValues.description === undefined
                ? undefined
                : formValues.description.length > 2
                    ? true
                    : false,
        });
    }, [formValues]);
    const nameSubmit = async () => {
        runStore.layout().showBackdrop();
        datasource.name = formValues.name;
        datasource.description = formValues.description;
        updateDatasourceMutation.mutate(datasource, {
            onSuccess: (resp) => {
                if (resp.success) {
                    enqueueToast('The datasource is successfully updated.', {
                        variant: 'success',
                    });
                    setDatasource(datasource);
                }
            },
            onSettled: () => {
                runStore.layout().hideBackdrop();
            },
        });
    };
    return (_jsxs(_Fragment, { children: [_jsx(PageHeader, { children: _jsx(Heading, { sx: { fontSize: 3 }, children: "Datasource" }) }), _jsxs(Box, { display: "flex", children: [_jsxs(Box, { children: [_jsx(BoringAvatar, { displayName: datasource?.name, size: 100, style: { paddingRight: 10 } }), _jsx(Text, { as: "h2", sx: { paddingTop: 3 }, children: datasource?.name }), _jsx(Box, { mt: 3, children: _jsx(Label, { size: "large", children: datasource?.variant }) })] }), _jsx(Box, { ml: 10, children: _jsxs(Box, { sx: { label: { marginTop: 2 } }, children: [_jsxs(FormControl, { children: [_jsx(FormControl.Label, { children: "Name" }), _jsx(TextInput, { block: true, value: formValues.name, onChange: nameNameChange }), validationResult.name === false && (_jsx(FormControl.Validation, { variant: "error", children: "Name must have more than 2 characters." }))] }), _jsxs(FormControl, { children: [_jsx(FormControl.Label, { children: "Description" }), _jsx(Textarea, { block: true, value: formValues.description, onChange: nameDescriptionChange, rows: 5 }), validationResult.description === false && (_jsx(FormControl.Validation, { variant: "error", children: "Description must have more than 2 characters." }))] }), _jsxs(FormControl, { children: [_jsx(FormControl.Label, { children: "Database" }), _jsx(TextInput, { placeholder: "Database", monospace: true, contrast: true, size: "large", type: passwordVisibility ? 'text' : 'password', value: datasource?.database, trailingAction: _jsx(TextInput.Action, { onClick: () => {
                                                    setPasswordVisibility(!passwordVisibility);
                                                }, icon: passwordVisibility ? EyeClosedIcon : EyeIcon, "aria-label": passwordVisibility ? 'Hide database' : 'Reveal database', sx: { color: 'var(--fgColor-muted)' } }), sx: { overflow: 'visible' } })] }), _jsxs(FormControl, { children: [_jsx(FormControl.Label, { children: "Output bucket" }), _jsx(TextInput, { placeholder: "Output bucket", monospace: true, contrast: true, size: "large", type: passwordVisibility ? 'text' : 'password', value: datasource?.outputBucket, trailingAction: _jsx(TextInput.Action, { onClick: () => {
                                                    setPasswordVisibility(!passwordVisibility);
                                                }, icon: passwordVisibility ? EyeClosedIcon : EyeIcon, "aria-label": passwordVisibility
                                                    ? 'Hide output bucket'
                                                    : 'Reveal output bucket', sx: { color: 'var(--fgColor-muted)' } }), sx: { overflow: 'visible' } })] }), _jsx(Button, { variant: "primary", disabled: !validationResult.name || !validationResult.description, sx: { marginTop: 3 }, onClick: nameSubmit, children: "Update datasource" })] }) })] })] }));
};
export default DatasourceDetail;
