import { type ElementType, useState } from 'react';

import { Button, Flex, TwoButtonDialog } from '@quotalab/qds';

import { OverlayProvider, useOverlay } from '.';

export default {
  title: 'Overlay Manager / Hook',
  decorators: [
    (Story: ElementType) => (
      <OverlayProvider>
        <Story />
      </OverlayProvider>
    ),
  ],
};

export function Default() {
  const { open, close, overlayList } = useOverlay();

  return (
    <Flex direction="column">
      <Button
        onClick={() => {
          open(({ overlayId }) => {
            return (
              <p style={{ cursor: 'pointer' }} onClick={() => close(overlayId)}>
                Overlay Component # {overlayId}
              </p>
            );
          });
        }}
      >
        open content modal
      </Button>
      <Flex direction="column">
        <p>overlayList</p>
        <ul>
          {overlayList.map((item) => {
            return <li key={item.id}>id: {item.id}</li>;
          })}
        </ul>
      </Flex>
    </Flex>
  );
}

export function RemovePrevModal() {
  const [overlayId, setOverlayId] = useState<string>();
  const { open, close, overlayList } = useOverlay();

  return (
    <Flex direction="column">
      <Button
        onClick={() => {
          if (overlayId != null) close(overlayId);

          open(({ overlayId }) => {
            setOverlayId(overlayId);
            return (
              <p style={{ cursor: 'pointer' }} onClick={() => close(overlayId)}>
                Overlay Component # {overlayId}
              </p>
            );
          });
        }}
      >
        open content modal
      </Button>
      <Flex direction="column">
        <p>overlayList</p>
        <ul>
          {overlayList.map((item) => {
            return <li key={item.id}>id: {item.id}</li>;
          })}
        </ul>
      </Flex>
    </Flex>
  );
}

export function OpenOverlay() {
  const { open, overlayList } = useOverlay();

  return (
    <Flex direction="column">
      <Button
        onClick={() => {
          open(({ isOpen, onClose }) => {
            return <TwoButtonDialog isOpen={isOpen} onClose={onClose} title="TwoButtonDialog" />;
          });
        }}
      >
        openOverlay
      </Button>
      <Flex direction="column">
        <p>overlayList</p>
        <ul>
          {overlayList.map((item) => {
            return <li key={item.id}>id: {item.id}</li>;
          })}
        </ul>
      </Flex>
    </Flex>
  );
}
