import React from "react";
import { Meta } from "@storybook/react";
import { DropDownTreeProps } from "./DropDownTreeProps";
import DropDownTree from "./DropDownTree";

import {
  processTreeData,
  expandedState,
} from "./mockData/tree-data-operations";
import { data } from "./mockData/tree-data";

import {
  DropDownTreeChangeEvent as KendoDropDownTreeChangeEvent,
  DropDownTreeExpandEvent as KendoDropDownTreeExpandEvent,
} from "@progress/kendo-react-dropdowns";

const selectField = "selected";
const expandField = "expanded";
const dataItemKey = "id";
const textField = "text";
const subItemsField = "items";
const fields = { selectField, expandField, dataItemKey, subItemsField };

export default {
  title: "Design System/Dropdowns/DropDownTree",
  component: DropDownTree,
  tags: ["autodocs"],
  parameters: {
    docs: {
      description: {
        component:
          'The KendoReact DropDownTree is a form component that lets you choose a single predefined value from a hierarchical list and is a richer version of the select element. Supports filtering, custom rendering, keyboard navigation, expand and collapse of the hierarchical data items. \n\n```javascript\nimport { DropDownTree } from "@renault-ui-library"\n```',
      },
    },
  },
  argTypes: {
    dataTestId: {
      control: { type: "text" },
      description: "Specifies the data-test-id attribute for testing purposes.",
    },
    accessKey: {
      control: { type: "text" },
      description: "Specifies the accessKey of the DropDownTree.",
    },
    adaptive: {
      control: { type: "boolean" },
      description:
        "Providing different rendering of the popup element based on the screen dimensions.",
    },
    adaptiveTitle: {
      control: { type: "text" },
      description:
        "Specifies the text that is rendered as title in the adaptive popup.",
    },
    ariaDescribedBy: {
      control: { type: "text" },
      description:
        "Identifies the element(s) which will describe the component, similar to HTML aria-describedby attribute.",
    },
    ariaLabelledBy: {
      control: { type: "text" },
      description: "Identifies the element(s) which will label the component.",
    },
    className: {
      control: { type: "text" },
      description: "Sets additional classes to the DropDownTree.",
    },
    data: {
      control: { type: "object" },
      description: "Sets the data of the DropDownTree.",
    },
    dataItemKey: {
      control: { type: "text" },
      description:
        "Sets the key for comparing the data items of the DropDownTree.",
    },
    dir: {
      control: { type: "text" },
      description: "Represents the dir HTML attribute.",
    },
    disabled: {
      control: { type: "boolean" },
      description: "Sets the disabled state of the DropDownTree.",
    },
    expandField: {
      control: { type: "text" },
      description:
        "Specifies the name of the field which will provide a Boolean representation of the expanded state of the item.",
    },
    fillMode: {
      control: {
        type: "select",
        options: ["null", "flat", "outline", "solid"],
      },
      description: "Configures the fillMode of the DropDownTree.",
    },
    filter: {
      control: { type: "text" },
      description: "Sets the value of filtering input.",
    },
    filterable: {
      control: { type: "boolean" },
      description: "Enables the filtering functionality of the DropDownTree.",
    },
    id: {
      control: { type: "text" },
      description: "Specifies the id of the component.",
    },
    item: {
      control: { type: "object" },
      description:
        "Defines the component that will be used for rendering each of the DropDownTree items.",
    },
    label: {
      control: { type: "text" },
      description: "Renders a floating label for the DropDownTree.",
    },
    listNoData: {
      control: { type: "object" },
      description:
        "Defines the component that will be rendered in the DropDownTree popup when no data is available.",
    },
    loading: {
      control: { type: "boolean" },
      description: "Sets the loading state of the DropDownTree.",
    },
    name: {
      control: { type: "text" },
      description: "Specifies the name property of the input DOM element.",
    },
    opened: {
      control: { type: "boolean" },
      description: "Sets the opened state of the DropDownTree.",
    },
    placeholder: {
      control: { type: "text" },
      description: "The hint that is displayed when the DropDownTree is empty.",
    },
    popupSettings: {
      control: { type: "object" },
      description: "Configures the popup of the DropDownTree.",
    },
    required: {
      control: { type: "boolean" },
      description: "Specifies if null is a valid value for the component.",
    },
    rounded: {
      control: {
        type: "select",
        options: ["null", "small", "medium", "full", "large"],
      },
      description: "Configures the roundness of the DropDownTree.",
    },
    selectField: {
      control: { type: "text" },
      description:
        "Specifies the name of the field which will provide a Boolean representation of the selected state of the item.",
    },
    size: {
      control: {
        type: "select",
        options: ["null", "small", "medium", "large"],
      },
      description: "Configures the size of the DropDownTree.",
    },
    style: {
      control: { type: "object" },
      description: "The styles that are applied to the DropDownTree.",
    },
    subItemsField: {
      control: { type: "text" },
      description:
        "Specifies the name of the field which will provide an array representation of the item subitems.",
    },
    tabIndex: {
      control: { type: "number" },
      description: "Specifies the tabIndex of the DropDownTree.",
    },
    textField: {
      control: { type: "text" },
      description: "Sets the data item field that represents the item text.",
    },
    valid: {
      control: { type: "boolean" },
      description: "Overrides the validity state of the component.",
    },
    validationMessage: {
      control: { type: "text" },
      description: "Controls the form error message of the component.",
    },
    validityStyles: {
      control: { type: "boolean" },
      description:
        "If set to false, no visual representation of the invalid state of the component will be applied.",
    },
    value: {
      control: { type: "object" },
      description: "Sets the value of the DropDownTree.",
    },
    valueHolder: {
      control: { type: "object" },
      description:
        "Defines the component that will be used for rendering the selected value.",
    },
    onBlur: {
      control: { type: "function" },
      description: "Fires each time the DropDownTree gets blurred.",
    },
    onChange: {
      control: { type: "function" },
      description:
        "Fires each time the value of the DropDownTree is about to change.",
    },
    onClose: {
      control: { type: "function" },
      description:
        "Fires each time the popup of the DropDownTree is about to close.",
    },
    onExpandChange: {
      control: { type: "function" },
      description:
        "Fires when the expanding or collapsing of an item is requested.",
    },
    onFilterChange: {
      control: { type: "function" },
      description: "Fires each time the user types in the filter input.",
    },
    onFocus: {
      control: { type: "function" },
      description: "Fires each time the user focuses the DropDownTree.",
    },
    onOpen: {
      control: { type: "function" },
      description:
        "Fires each time the popup of the DropDownTree is about to open.",
    },
  },
} as Meta;

export const Default = (args: DropDownTreeProps): JSX.Element => {
  const [value, setValue] = React.useState({ text: "Tables & Chairs", id: 2 });
  const [expanded, setExpanded] = React.useState([data[0][dataItemKey]]);

  const onChange = (event: KendoDropDownTreeChangeEvent) =>
    setValue(event.value);
  const onExpandChange = React.useCallback(
    (event: KendoDropDownTreeExpandEvent) =>
      setExpanded(
        (
          expandedState(event.item, dataItemKey, expanded) as (
            | string
            | number
          )[]
        ).map((item) => Number(item) as number)
      ),
    [expanded]
  );

  const treeData = React.useMemo(
    () =>
      processTreeData(
        data,
        {
          expanded,
          value,
          filter: undefined,
        },
        fields
      ),
    [expanded, value]
  );

  return (
    <DropDownTree
      style={args.style}
      data={treeData}
      value={value}
      onChange={onChange}
      placeholder={args.placeholder}
      textField={textField}
      dataItemKey={dataItemKey}
      selectField={selectField}
      expandField={expandField}
      onExpandChange={onExpandChange}
    />
  );
};

Default.args = {
  dataTestId: "dropdown-tree-data-testid",
  style: { width: 300 },
  placeholder: "Please select ...",
  fillMode: "solid",
  rounded: "medium",
  size: "large",
};
