import React, { Component, createContext, useContext } from "react";
import { createSubscriptionHook } from "@ywfe/hook-subscription";
import {
  YSystemProps,
  YSystemState,
  SystemContextProps,
} from "./system.interface";
import {SystemContext} from './system.context';
import {reducer, initialState} from './system.reducer';

export class YSystem extends Component<YSystemProps, YSystemState> {

  constructor(props: YSystemProps) {
    super(props);
    this.state = {
      store: createSubscriptionHook(reducer, initialState)
    };
  }

  render() {
    const configObject = {
      store: this.state.store,
      history: this.props.history,
      moduleId: this.props.moduleId,
      moduleName: this.props.moduleName
    };
    const {children} = this.props;

    const childs = React.Children.map(children, (child) => {
      
      return React.cloneElement(child as React.FunctionComponentElement<SystemContextProps>);
    });

    return (
      <SystemContext.Provider value={configObject}>
       {childs}
      </SystemContext.Provider>
    );
  }
}
