import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
var _excluded = ["prefixCls", "className", "theme", "collapsible", "reverseArrow", "trigger", "style", "width", "collapsedWidth", "children"];
import React, { PureComponent } from 'react';
import classNames from 'classnames';
import omit from 'lodash/omit';
import isNumber from 'lodash/isNumber';
import Icon from '../icon';
import { matchMediaPolifill } from '../_util/mediaQueryListPolyfill';
import LayoutContext from './LayoutContext';
import { LayoutSiderContextProvider } from './LayoutSiderContext';

if (typeof window !== 'undefined') {
  // const matchMediaPolyfill = (mediaQuery: string): MediaQueryList => {
  //   return {
  //     media: mediaQuery,
  //     matches: false,
  //     addListener() {
  //     },
  //     removeListener() {
  //     },
  //   };
  // };
  window.matchMedia = window.matchMedia || matchMediaPolifill;
}

var dimensionMap = {
  xs: '480px',
  sm: '576px',
  md: '768px',
  lg: '992px',
  xl: '1200px',
  xxl: '1600px'
};

var generateId = function () {
  var i = 0;
  return function () {
    var prefix = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
    i += 1;
    return "".concat(prefix).concat(i);
  };
}();

var Sider = /*#__PURE__*/function (_PureComponent) {
  _inherits(Sider, _PureComponent);

  var _super = _createSuper(Sider);

  function Sider(props, context) {
    var _this;

    _classCallCheck(this, Sider);

    _this = _super.call(this, props, context);

    _this.responsiveHandler = function (event) {
      _this.setState({
        below: event.matches
      });

      var onBreakpoint = _this.props.onBreakpoint;

      if (onBreakpoint) {
        onBreakpoint(event.matches);
      }

      var collapsed = _this.state.collapsed;

      if (collapsed !== event.matches) {
        _this.setCollapsed(event.matches, 'responsive');
      }
    };

    _this.setCollapsed = function (collapsed, type) {
      if (!('collapsed' in _this.props)) {
        _this.setState({
          collapsed: collapsed
        });
      }

      var onCollapse = _this.props.onCollapse;

      if (onCollapse) {
        onCollapse(collapsed, type);
      }
    };

    _this.toggle = function () {
      var collapsed = _this.state.collapsed;

      _this.setCollapsed(!collapsed, 'clickTrigger');
    };

    _this.belowShowChange = function () {
      var belowShow = _this.state.belowShow;

      _this.setState({
        belowShow: !belowShow
      });
    };

    var getPrefixCls = context.getPrefixCls;
    _this.uniqueId = generateId(getPrefixCls('sider-'));
    var matchMedia;

    if (typeof window !== 'undefined') {
      matchMedia = window.matchMedia;
    }

    if (matchMedia && props.breakpoint && props.breakpoint in dimensionMap) {
      _this.mql = matchMedia("(max-width: ".concat(dimensionMap[props.breakpoint], ")"));
    }

    var collapsed;

    if ('collapsed' in props) {
      collapsed = props.collapsed;
    } else {
      collapsed = props.defaultCollapsed;
    }

    _this.state = {
      collapsed: collapsed,
      below: false
    };
    return _this;
  }

  _createClass(Sider, [{
    key: "getContextValue",
    value: function getContextValue() {
      var collapsedWidth = this.props.collapsedWidth;
      var collapsed = this.state.collapsed;
      return {
        siderCollapsed: collapsed,
        collapsedWidth: collapsedWidth
      };
    }
  }, {
    key: "componentWillReceiveProps",
    value: function componentWillReceiveProps(nextProps) {
      if ('collapsed' in nextProps) {
        this.setState({
          collapsed: nextProps.collapsed
        });
      }
    }
  }, {
    key: "componentDidMount",
    value: function componentDidMount() {
      if (this.mql) {
        this.mql.addListener(this.responsiveHandler);
        this.responsiveHandler(new MediaQueryListEvent('change', {
          matches: this.mql.matches,
          media: this.mql.media
        }));
      }

      var siderHook = this.context.siderHook;

      if (siderHook) {
        siderHook.addSider(this.uniqueId);
      }
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      if (this.mql) {
        this.mql.removeListener(this.responsiveHandler);
      }

      var siderHook = this.context.siderHook;

      if (siderHook) {
        siderHook.removeSider(this.uniqueId);
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _classNames;

      var _this$props = this.props,
          customizePrefixCls = _this$props.prefixCls,
          className = _this$props.className,
          theme = _this$props.theme,
          collapsible = _this$props.collapsible,
          reverseArrow = _this$props.reverseArrow,
          trigger = _this$props.trigger,
          style = _this$props.style,
          width = _this$props.width,
          collapsedWidth = _this$props.collapsedWidth,
          children = _this$props.children,
          others = _objectWithoutProperties(_this$props, _excluded);

      var _this$state = this.state,
          collapsed = _this$state.collapsed,
          below = _this$state.below;
      var getPrefixCls = this.context.getPrefixCls;
      var prefixCls = getPrefixCls('layout-sider', customizePrefixCls);
      var divProps = omit(others, ['collapsed', 'defaultCollapsed', 'onCollapse', 'breakpoint', 'onBreakpoint']);
      var rawWidth = collapsed ? collapsedWidth : width; // use "px" as fallback unit for width

      var siderWidth = isNumber(rawWidth) ? "".concat(rawWidth, "px") : String(rawWidth); // special trigger when collapsedWidth == 0

      var zeroWidthTrigger = parseFloat(String(collapsedWidth || 0)) === 0 ? /*#__PURE__*/React.createElement("span", {
        onClick: this.toggle,
        className: "".concat(prefixCls, "-zero-width-trigger")
      }, /*#__PURE__*/React.createElement(Icon, {
        type: "bars"
      })) : null;
      var iconObj = {
        expanded: reverseArrow ? /*#__PURE__*/React.createElement(Icon, {
          type: "right"
        }) : /*#__PURE__*/React.createElement(Icon, {
          type: "left"
        }),
        collapsed: reverseArrow ? /*#__PURE__*/React.createElement(Icon, {
          type: "left"
        }) : /*#__PURE__*/React.createElement(Icon, {
          type: "right"
        })
      };
      var status = collapsed ? 'collapsed' : 'expanded';
      var defaultTrigger = iconObj[status];
      var triggerDom = trigger !== null ? zeroWidthTrigger || /*#__PURE__*/React.createElement("div", {
        className: "".concat(prefixCls, "-trigger"),
        onClick: this.toggle,
        style: {
          width: siderWidth
        }
      }, trigger || defaultTrigger) : null;

      var divStyle = _objectSpread(_objectSpread({}, style), {}, {
        flex: "0 0 ".concat(siderWidth),
        maxWidth: siderWidth,
        minWidth: siderWidth,
        width: siderWidth
      });

      var siderCls = classNames(className, prefixCls, "".concat(prefixCls, "-").concat(theme), (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-collapsed"), !!collapsed), _defineProperty(_classNames, "".concat(prefixCls, "-has-trigger"), collapsible && trigger !== null && !zeroWidthTrigger), _defineProperty(_classNames, "".concat(prefixCls, "-below"), !!below), _defineProperty(_classNames, "".concat(prefixCls, "-zero-width"), parseFloat(siderWidth) === 0), _classNames));
      return /*#__PURE__*/React.createElement(LayoutSiderContextProvider, _extends({}, this.getContextValue(), {
        getPrefixCls: getPrefixCls
      }), /*#__PURE__*/React.createElement("div", _extends({
        className: siderCls
      }, divProps, {
        style: divStyle
      }), /*#__PURE__*/React.createElement("div", {
        className: "".concat(prefixCls, "-children")
      }, children), collapsible || below && zeroWidthTrigger ? triggerDom : null));
    }
  }], [{
    key: "contextType",
    get: function get() {
      return LayoutContext;
    }
  }]);

  return Sider;
}(PureComponent);

export { Sider as default };
Sider.displayName = 'LayoutSider';
Sider.__C7N_LAYOUT_SIDER = true;
Sider.defaultProps = {
  collapsible: false,
  defaultCollapsed: false,
  reverseArrow: false,
  width: 200,
  collapsedWidth: 80,
  style: {},
  theme: 'dark'
};
//# sourceMappingURL=Sider.js.map
