import React, { Component } from "react";
import ReactDOM from "react-dom";
import PropTypes from "prop-types";
import Container from "@zeecoder/container-query";
import objectAssign from "object-assign";

export default class ContainerQuery extends Component {
  constructor(props) {
    super(props);

    this.state = { size: null };

    this.handleResize = this.handleResize.bind(this);

    this.containerOptions = objectAssign({}, this.props.options);

    // Listen to size changes only if needed
    if (
      typeof this.props.render === "function" ||
      typeof this.props.children === "function"
    ) {
      this.containerOptions.handleResize = this.handleResize;
    }
  }

  handleResize(size) {
    if (this.unMounting) {
      return;
    }

    this.setState({ size });
  }

  componentDidMount() {
    if (!this.props.stats) {
      return;
    }

    this.lastContainer = ReactDOM.findDOMNode(this);
    new Container(this.lastContainer, this.props.stats, this.containerOptions);
  }

  componentDidUpdate() {
    if (!this.props.stats) {
      return;
    }

    const element = ReactDOM.findDOMNode(this);
    if (this.lastContainer !== element) {
      this.lastContainer = element;
      new Container(
        this.lastContainer,
        this.props.stats,
        this.containerOptions
      );
    }
  }

  componentWillUnmount() {
    this.unMounting = true;
  }

  render() {
    if (typeof this.props.children === "function") {
      return this.props.children(this.state.size);
    } else if (this.props.children) {
      return this.props.children;
    }

    return this.props.render(this.state.size);
  }
}

ContainerQuery.defaultProps = {
  stats: {},
  options: {}
};

ContainerQuery.propTypes = {
  render: PropTypes.func,
  stats: PropTypes.object,
  options: PropTypes.object,
  children: PropTypes.oneOfType([PropTypes.element, PropTypes.func])
};

@@ -814,32 +814,52 @@
 unt() %7B%0A    if (
+!this.props.meta && 
 !this.props.stat
@@ -933,24 +933,78 @@
 Node(this);%0A
+    const meta = this.props.meta %7C%7C this.props.stats;%0A
     new Cont
@@ -1029,32 +1029,20 @@
 tainer, 
-this.props.stats
+meta
 , this.c
@@ -1089,32 +1089,52 @@
 ate() %7B%0A    if (
+!this.props.meta && 
 !this.props.stat
@@ -1155,24 +1155,79 @@
 urn;%0A    %7D%0A%0A
+    const meta = this.props.meta %7C%7C this.props.stats;%0A%0A
     const el
@@ -1360,25 +1360,16 @@
 ntainer(
-%0A        
 this.las
@@ -1383,42 +1383,14 @@
 ner,
-%0A        this.props.stats,%0A       
+ meta,
  thi
@@ -1407,23 +1407,16 @@
 rOptions
-%0A      
 );%0A    %7D
@@ -1776,16 +1776,28 @@
 ts: %7B%7D,%0A
+  meta: %7B%7D,%0A
   option
@@ -1888,16 +1888,42 @@
 object,%0A
+  meta: PropTypes.object,%0A
   option

support for meta prop