import _Object$getOwnPropertyDescriptor from "@babel/runtime-corejs2/core-js/object/get-own-property-descriptor";
import "core-js/modules/es6.number.constructor";
import _Object$assign from "@babel/runtime-corejs2/core-js/object/assign";
import _Array$isArray from "@babel/runtime-corejs2/core-js/array/is-array";
import "core-js/modules/es6.array.find";
import _createClass from "@babel/runtime-corejs2/helpers/createClass";
import _inheritsLoose from "../../inheritsLoose";
import _defineProperty from "@babel/runtime-corejs2/helpers/defineProperty";
import _applyDecoratedDescriptor from "@babel/runtime-corejs2/helpers/applyDecoratedDescriptor";

var _dec, _class, _class2, _temp;

import Intact from 'intact-angular';
import template from './row.vdt';
import mediaQuery from './mediaQuery';
import { isObject, hasWindow } from '../utils';
import Col from './col';
import { breakpoints } from './utils';
import matchMedia from './mediaMatch';
import '../../styles/kpc.styl';
import './index.styl';
var enquire;

if (hasWindow) {
  window.matchMedia = matchMedia();
  enquire = require('enquire.js');
}

var responsiveMap = {
  xxl: "(min-width: 0)",
  xl: "(min-width: " + mediaQuery.xlMinWidth + ")",
  lg: "(min-width: " + mediaQuery.lgMinWidth + ")",
  md: "(min-width: " + mediaQuery.mdMinWidth + ")",
  sm: "(min-width: " + mediaQuery.smMinWidth + ")",
  xs: "(max-width: " + mediaQuery.xsMaxWidth + ")"
};
var Row = (_dec = Intact.template(), (_class = (_temp = _class2 =
/*#__PURE__*/
function (_Intact) {
  _inheritsLoose(Row, _Intact);

  function Row() {
    return _Intact.apply(this, arguments) || this;
  }

  var _proto = Row.prototype;

  _proto.defaults = function defaults() {
    return {
      gutter: 0,
      justify: undefined,
      flex: false,
      align: undefined,
      _gutter: undefined
    };
  };

  _proto._init = function _init() {
    var _this = this;

    this.useFlex = false;
    var needKeys = ['justify', 'align', 'flex', 'children'];
    this.on("$receive", function (c, keys) {
      if (needKeys.find(function (key) {
        return keys.indexOf(key) > -1;
      })) {
        var _this$get = _this.get(),
            flex = _this$get.flex,
            justify = _this$get.justify,
            align = _this$get.align,
            children = _this$get.children;

        _this.useFlex = flex || justify || align; // detect the order property of children

        if (!_this.useFlex && children) {
          var _children = children;

          if (!_Array$isArray(children)) {
            _children = [children];
          }

          for (var i = 0; i < _children.length; i++) {
            var vNode = _children[i];

            if (vNode && vNode.tag === Col) {
              var props = vNode.props;

              if (props.order != null) {
                _this.useFlex = true;
              } else {
                for (var _i = 0; _i < breakpoints.length; _i++) {
                  var breakpoint = breakpoints[_i];

                  if (props[breakpoint] && props[breakpoint].order != null) {
                    _this.useFlex = true;
                    break;
                  }
                }
              }

              if (_this.useFlex) break;
            }
          }
        }
      }
    });
  };

  _proto._create = function _create() {
    var _this2 = this;

    var _loop = function _loop(key) {
      enquire.register(responsiveMap[key], {
        match: function match() {
          var _this2$get = _this2.get(),
              gutter = _this2$get.gutter;

          if (isObject(gutter)) {
            var _Object$assign2;

            _this2.set('_matches', _Object$assign({}, _this2.get('_matches'), (_Object$assign2 = {}, _Object$assign2[key] = true, _Object$assign2)), {
              async: true
            });
          }
        },
        unmatch: function unmatch() {
          var _this2$get2 = _this2.get(),
              gutter = _this2$get2.gutter;

          if (isObject(gutter)) {
            var _Object$assign3;

            _this2.set('_matches', _Object$assign({}, _this2.get('_matches'), (_Object$assign3 = {}, _Object$assign3[key] = false, _Object$assign3)), {
              async: true
            });
          }
        }
      });
    };

    for (var key in responsiveMap) {
      _loop(key);
    }
  };

  _proto._getGutter = function _getGutter() {
    var _this$get2 = this.get(),
        gutter = _this$get2.gutter;

    if (isObject(gutter)) {
      for (var i = 0; i < breakpoints.length; i++) {
        var breakpoint = breakpoints[i];

        if (this.get("_matches." + breakpoint) && gutter[breakpoint] != null) {
          return gutter[breakpoint];
        }
      }

      return 0;
    }

    return gutter;
  };

  _createClass(Row, [{
    key: "template",
    get: function get() {
      return template;
    }
  }]);

  return Row;
}(Intact), _defineProperty(_class2, "propTypes", {
  gutter: [String, Number, Object],
  justify: ['start', 'end', 'center', 'between', 'around', 'evenly'],
  align: ['top', 'bottom', 'middle', 'stretch', 'baseline'],
  flex: Boolean
}), _temp), (_applyDecoratedDescriptor(_class.prototype, "template", [_dec], _Object$getOwnPropertyDescriptor(_class.prototype, "template"), _class.prototype)), _class));
export { Row as default };