/* eslint-disable react-hooks/rules-of-hooks */
import { useCallback, useState } from "react";

import { Button } from "../button";
import { Flex } from "../flex";
import { commonProps } from "../../utils/storybook";
import { ShowMoreText } from ".";

import type { StoryObj, Meta } from "@storybook/react";

const meta: Meta<typeof ShowMoreText> = {
  title: "ShowMoreText",
  component: ShowMoreText,
  tags: ["autodocs"],
  argTypes: {
    ...commonProps,
  },
  args: {
    triggerCollapsedText: "Show more",
    triggerExpandedText: "Show less",
    rowsToShow: 3,
    content:
      "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
  },
};

export default meta;

type Story = StoryObj<typeof ShowMoreText>;

export const Uncontrolled: Story = {
  render: (args) => <ShowMoreText {...args} />,
};

export const Controlled: Story = {
  render: (args) => {
    const [isExpanded, setIsExpanded] = useState(false);
    const onClick = useCallback(() => {
      setIsExpanded((prev) => !prev);
    }, []);

    const onExpandedChange = useCallback((isExpanded: boolean) => {
      setIsExpanded(isExpanded);
    }, []);

    return (
      <Flex gap="2" direction="column">
        <ShowMoreText {...args} isExpanded={isExpanded} onExpandedChange={onExpandedChange} />
        <Button onClick={onClick}>Toggle</Button>
      </Flex>
    );
  },
};
