import { Meta } from "@storybook/react";
import { EventDataProps, TimelineProps } from "./TimelineProps";
import Timeline from "./Timeline";
import { sortEventList as KendoSortEventList } from "@progress/kendo-react-layout";
import { events } from "./mockData/events";
const sortedEvents = KendoSortEventList(events);

export default {
  title: "Design System/Layout/Timeline",
  component: Timeline,
  tags: ["autodocs"],
  parameters: {
    docs: {
      description: {
        component:
          'The Timeline provides a way to display a collection of events and their data in chronological order. \n\n```javascript\nimport { Timeline } from "@renault-ui-library"\n```',
      },
    },
  },
  argTypes: {
    dataTestId: {
      control: { type: "text" },
      description: "Specifies the data-test-id attribute for testing purposes.",
    },
    alterMode: {
      control: { type: "boolean" },
      description: "Render events alternatingly on both sides of the axis.",
    },
    className: {
      control: { type: "text" },
      description:
        "Specifies the CSS class names which are set to the Timeline.",
    },
    collapsibleEvents: {
      control: { type: "boolean" },
      description:
        "Specifies option for expanding and collapsing the event card.",
    },
    dateFormat: {
      control: { type: "text" },
      description: "The date format for displaying the event date.",
    },
    events: {
      control: { type: "object" },
      description: "Array of TimelineEventProps.",
    },
    navigatable: {
      control: { type: "boolean" },
      description:
        "If set to true, the user can use dedicated shortcuts to interact with the Timeline.",
    },
    transitionDuration: {
      control: { type: "number" },
      description:
        "Specifies the time for sliding to next event in horizontal mode and time for collapsing the event in vertical mode.",
    },
    onActionClick: {
      control: { type: "function" },
      description: "An event that is called when card action is clicked.",
    },
    onChange: {
      control: { type: "function" },
      description: "An event that is called when event card is toggled.",
    },
  },
} as Meta;

export const Default = (args: TimelineProps): JSX.Element => {
  const onActionClick = (event: EventDataProps) => {
    const syntheticEvent = event.syntheticEvent;
    if (syntheticEvent) {
      syntheticEvent.preventDefault();
      const targetElement = syntheticEvent.target as HTMLElement;
      const href = targetElement.getAttribute("href");
      if (href) {
        window.open(href);
      }
    }
  };

  return (
    <Timeline
      events={args.events}
      alterMode={args.alterMode}
      collapsibleEvents={args.collapsibleEvents}
      onActionClick={onActionClick}
    />
  );
};

Default.args = {
  dataTestId: "timeline-data-testid",
  events: sortedEvents,
  alterMode: true,
  collapsibleEvents: true,
};
