// 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 * as React from 'react';
import { useToastController } from "@coreai-ds/react-components";
import { Toast } from "@coreai-ds/react-components";
import { ToastTitle } from "@coreai-ds/react-components";
import { Toaster } from "@coreai-ds/react-components";
import { Button } from "@coreai-ds/react-components";

export const DismissToast = (): JSXElement => {
    const toasterId = useId('toaster');
    const toastId = useId('example');
    const [unmounted, setUnmounted] = React.useState(true);
    const { dispatchToast, dismissToast } = useToastController(toasterId);
    const notify = () => {
        dispatchToast(<Toast>
        <ToastTitle>This is a toast</ToastTitle>
      </Toast>, { toastId, intent: 'success', onStatusChange: (e, { status }) => setUnmounted(status === 'unmounted') });
        setUnmounted(false);
    };
    const dismiss = () => dismissToast(toastId);
    return (<>
      <Toaster toasterId={toasterId}/>
      <Button onClick={unmounted ? notify : dismiss}>{unmounted ? 'Make' : 'Dismiss'} toast</Button>
    </>);
};

export default DismissToast;
