import React, { useEffect } from 'react';
import { guid, mergeObject, SysModel } from '../utils';
import { getNodeStyle } from '../utils/style';
import JsCss, { plugin } from './jscss';
import { PREFIX_CLS } from '../config';
import './style/index';

const prefixCls = `${PREFIX_CLS}-box`;
const Box = ({ model, data, dispatch, children }) => {
  const { style } = mergeObject(plugin, data);
  const onLoad = () => {
    const values = [
      {
        type: 'VerLayout',
        id: guid(),
        style: {
          width: '100%',
          height: '100%',
        },
        props: {},
        pid: data.id,
        children: [],
      },
    ];
    if (data.children.length === 0) {
      dispatch({
        type: 'tree/updateByKey',
        payload: {
          key: 'children',
          value: values,
          id: data.id,
          createHistory: false,
        },
      });
      dispatch({
        type: 'tree/select',
        payload: { data: values[0] },
      });
    }
  };
  useEffect(() => {
    if (model === SysModel.DEV) {
      onLoad();
    }
  }, []);
  return (
    <JsCss
      style={getNodeStyle(style)}
      styles={style}
      className={`${prefixCls}`}
    >
      {children}
    </JsCss>
  );
};

export default Box;
