import React from 'react';

import { autobind } from 'core-decorators';

import PageConfig from './config';
import customConfig from './customConfig';
import { longRunExec } from '../system/longRunOpt';
import { PFetch } from '../system/fetch';

const createPage = (domainType, domainLink, query) => {
  const page = customConfig(domainLink, query) ? customConfig(domainLink, query) : PageConfig[domainType];
  return page || PageConfig.default;
};

const InitDecorator = () => {
  class WrapperComponent extends React.Component {
    constructor(props) {
      super(props);
      this.state = {
        data: null
      };
    }

    @autobind
    _createRefs() {
      const ref = 'pageContainer_' + Math.random();
      this.refStr = ref;
      return ref;
    }

    componentDidMount() {
      // remove loading if exist
    }

    render() {
      const Wrapper = createPage(this.props.domainType, this.props.domainLink, this.props.query);
      return (
        <Wrapper
          {...this.props}
          ref={this._createRefs}
          exec={longRunExec}
          fetch={PFetch}
          resetPosition={this.props.resetPosition}
          initWrapperHeight={this.props.initWrapperHeight}
          bodyAutoScroll={this.props.bodyAutoScroll}
        />
      );
    }
  }

  return WrapperComponent;
};

export default InitDecorator;

