import React, { useState, useContext, useEffect } from "react";
import generateState, { onIteratedState, onGetStatus, onExpandToggle } from "./TreeViewer.genarateStateStructure";
import TreeViewerContext from "./TreeViewerContext";
import SkeletonThemeWrapper from "../../customization-cornor/skeletonCustomization";
export default function TreeViewerBehaviour(props) {
  let renderProps = {};
  let {
    renderComponent,
    ...basicProps
  } = props;
  let {
    children
  } = basicProps;
  let [treeUIState, onTreeStateChange] = useState(generateState(children));

  function handleChange(id, status) {
    let iteratedState = onIteratedState(id, status, treeUIState);
    onTreeStateChange(iteratedState);
  }

  useEffect(() => {
    console.log(treeUIState, "treeUIState");
    let newStatus = props.needExpandAll ? onExpandToggle(props.isExpandAll, treeUIState) : treeUIState;
    onTreeStateChange(newStatus);
  }, []);
  useEffect(() => {
    let newStatus = props.needExpandAll ? onExpandToggle(props.isExpandAll, treeUIState) : treeUIState;
    onTreeStateChange(newStatus);
  }, [props.isExpandAll]);
  return /*#__PURE__*/React.createElement(TreeViewerContext.Provider, {
    value: {
      treeUIState,
      handleChange
    }
  }, /*#__PURE__*/React.createElement(SkeletonThemeWrapper, {
    basicProps: basicProps,
    renderProps: renderProps,
    component: renderComponent
  }));
} // TreeViewerBehaviour.propTypes = treeViewerPropTypes;
// TreeViewerBehaviour.defaultProps = defaultProps;
// TreeViewerBehaviour.propsDescription = propsDescription;

export let TreeItemBehaviour = props => {
  let {
    renderComponent,
    ...basicProps
  } = props;
  let {
    id
  } = basicProps;
  let treeState = useContext(TreeViewerContext);
  let {
    treeUIState,
    handleChange
  } = treeState;
  let status = onGetStatus(id, treeUIState);
  let renderProps = {
    treeUIState,
    handleChange,
    status
  };
  return /*#__PURE__*/React.createElement(SkeletonThemeWrapper, {
    basicProps: basicProps,
    renderProps: renderProps,
    component: renderComponent
  });
};