/**
 * Webpack has bug for import loop, which is not the same as ES module
 * When util.js imports the TreeNode for tree generate will casue treeContextTypes be empty
 */

import * as React from "react";
import {
  IconType,
  Key,
  DataEntity,
  EventDataNode,
  NodeInstance
} from "./interface";

//Node Mouse Event Handler
type NodeMouseEventHandler = (
  e: React.MouseEvent<HTMLDivElement>,
  node: EventDataNode
) => void;

//Node Drag Event Handler
type NodeDragEventHandler = (
  e: React.MouseEvent<HTMLDivElement>,
  node?: NodeInstance
) => void;

interface TreeContextProps {
  prefixCls: string;
  showIcon: boolean;
  icon: IconType;
  switcherIcon: IconType;
  selectable: boolean;
  draggable: boolean;
  disabled: boolean;
  keyEntities: Record<Key, DataEntity>;

  loadData: (treeNode: EventDataNode) => Promise<void>;
  filterTreeNode: (treeNode: EventDataNode) => boolean;
  onNodeLoad: (treeNode: EventDataNode) => void;

  onNodeClick: NodeMouseEventHandler;
  onNodeDoubleClick: NodeMouseEventHandler;
  onNodeExpand: NodeMouseEventHandler;
  onNodeSelect: NodeMouseEventHandler;
  onNodeMouseEnter: NodeMouseEventHandler;
  onNodeMouseLeave: NodeMouseEventHandler;
  onNodeContextMenu: NodeMouseEventHandler;
  onNodeDragStart: NodeDragEventHandler;
  onNodeDragEnter: NodeDragEventHandler;
  onNodeDragOver: NodeDragEventHandler;
  onNodeDragLeave: NodeDragEventHandler;
  onNodeDragEnd: NodeDragEventHandler;
  onNodeDrop: NodeDragEventHandler;
}

const TreeContext: React.Context<TreeContextProps | null> = React.createContext(
  null
);

export { TreeContextProps, TreeContext };
