// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import type { JSXElement } from "@coreai-ds/react-components";
import { useId } from "@coreai-ds/react-components";
import { useToastController } from "@coreai-ds/react-components";
import * as React from 'react';
import { Toast } from "@coreai-ds/react-components";
import { ToastTitle } from "@coreai-ds/react-components";
import { Field } from "@coreai-ds/react-components";
import { SpinButton } from "@coreai-ds/react-components";
import { Toaster } from "@coreai-ds/react-components";
import { Button } from "@coreai-ds/react-components";

export const ToasterLimit = (): JSXElement => {
    const toasterId = useId('toaster');
    const { dispatchToast } = useToastController(toasterId);
    const [limit, setLimit] = React.useState(3);
    const notify = () => dispatchToast(<Toast>
        <ToastTitle>Limited to 3 toasts</ToastTitle>
      </Toast>, { intent: 'success' });
    return (<>
      <Field label="Toaster Limit">
        <SpinButton value={limit} onChange={(e, data) => {
            if (data.value) {
                setLimit(data.value);
            }
            else if (data.displayValue !== undefined) {
                const newValue = parseFloat(data.displayValue);
                if (!Number.isNaN(newValue)) {
                    setLimit(newValue);
                }
            }
        }}/>
      </Field>
      <br />
      <Toaster toasterId={toasterId} limit={limit}/>
      <Button onClick={notify}>Make toast</Button>
    </>);
};

export default ToasterLimit;
