import React, {useState} from 'react';
import Switch, {SwitchControlled} from './Switch';

export default function SwitchConsumer ( props ) {  
  let isControlledByParent = props.onToggle;
  let swichComponent;
  if(isControlledByParent)
  {
    let [stateChecked,setChecked] = useState(props.checked);
    swichComponent = <Switch  {...props} checked={stateChecked} onToggle={()=> {
       console.log('Inside Switch controlled by parent '); 
      setChecked(!stateChecked)
    }}/>
  }
  else
  {
    swichComponent = <SwitchControlled  {...props} checked={props.checked} onChange={( value)=> console.log('from SwitchControlled' ,value)} />
  }
  return (
    <div className="composer">
        {swichComponent}
      </div>
  );
}

