import React from 'react';
import { StoryObj, Meta } from '@storybook/react';
import { useArgs } from '@storybook/preview-api';
import AUDialogModalComponent from '../src/components/AUDialogModalComponent';
import AUButtonComponent from '../src/components/AUButtonComponent';
import { ThemeWrapper } from './lib/helpers';
import { useAUDialogModalContext } from '../src/lib/context';

const ModalContent = () => {
  const close = useAUDialogModalContext();
  return (
    <>
      <h2 className="modal-view__header">
        The quick brown fox jumps over the lazy dog
      </h2>
      <div className="modal-view__content">
        <p>"The quick brown fox jumps over the lazy dog" is an English-language pangram—a sentence that contains all of the letters of the alphabet. It is commonly used for touch-typing practice, testing typewriters and computer keyboards, displaying examples of fonts, and other applications involving text where the use of all letters in the alphabet is desired. Owing to its brevity and coherence, it has become widely known.</p><h3>History</h3><p>The earliest known appearance of the phrase is from The Boston Journal. In an article titled "Current Notes" in the February 10, 1885, morning edition, the phrase is mentioned as a good practice sentence for writing students: "A favorite copy set by writing teachers for their pupils is the following, because it contains every letter of the alphabet: 'A quick brown fox jumps over the lazy dog.'"[1] Dozens of other newspapers published the phrase over the next few months, all using the version of the sentence starting with "A" rather than "The".[2] The earliest known use of the phrase in its modern form (starting with "The") is from the 1888 book Illustrative Shorthand by Linda Bronson.[3] The modern form (starting with "The") became more common despite the fact that it is slightly longer than the original (starting with "A").</p>
      </div>
      <AUButtonComponent
        label="Close modal"
        ariaHasPopup
        onClick={() => {
          if (typeof close === 'function') {
            close();
          }
        }}
      />
    </>
  );
}

// More on default export: https://storybook.js.org/docs/react/writing-stories/introduction#default-export
export default {
  title: 'Delphinus/Dialog Modal',
  component: AUDialogModalComponent,
  argTypes: {
    sender: {
      table: {
        disable: true,
      },
    },
  },
  decorators: [
    (Story, context) => (
      <ThemeWrapper theme={context.globals.theme}>
        {Story()}
      </ThemeWrapper>
    )
  ],
} as Meta<typeof AUDialogModalComponent>;

type Story = StoryObj<typeof AUDialogModalComponent>;

export const Default: Story = {
  args: {
    show: true,
    closeButtonDisabled: false,
  },
  render: (args) => {
    const [_, updateArgs] = useArgs();

    return (
      <div className="page__content__block">
        <AUButtonComponent
          label="Open modal"
          ariaHasPopup
          onClick={() => updateArgs({ ...args, show: true })}
        />
        <AUDialogModalComponent {...args} onClose={() => updateArgs({ ...args, show: false })}>
          <ModalContent />
        </AUDialogModalComponent>
      </div>
    );
  },
};
