"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));

var _react = _interopRequireWildcard(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _omit = _interopRequireDefault(require("lodash/omit"));

var _isNumber = _interopRequireDefault(require("lodash/isNumber"));

var _icon = _interopRequireDefault(require("../icon"));

var _mediaQueryListPolyfill = require("../_util/mediaQueryListPolyfill");

var _LayoutContext = _interopRequireDefault(require("./LayoutContext"));

var _LayoutSiderContext = require("./LayoutSiderContext");

var _excluded = ["prefixCls", "className", "theme", "collapsible", "reverseArrow", "trigger", "style", "width", "collapsedWidth", "children"];

if (typeof window !== 'undefined') {
  // const matchMediaPolyfill = (mediaQuery: string): MediaQueryList => {
  //   return {
  //     media: mediaQuery,
  //     matches: false,
  //     addListener() {
  //     },
  //     removeListener() {
  //     },
  //   };
  // };
  window.matchMedia = window.matchMedia || _mediaQueryListPolyfill.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) {
  (0, _inherits2["default"])(Sider, _PureComponent);

  var _super = (0, _createSuper2["default"])(Sider);

  function Sider(props, context) {
    var _this;

    (0, _classCallCheck2["default"])(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;
  }

  (0, _createClass2["default"])(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 = (0, _objectWithoutProperties2["default"])(_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 = (0, _omit["default"])(others, ['collapsed', 'defaultCollapsed', 'onCollapse', 'breakpoint', 'onBreakpoint']);
      var rawWidth = collapsed ? collapsedWidth : width; // use "px" as fallback unit for width

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

      var zeroWidthTrigger = parseFloat(String(collapsedWidth || 0)) === 0 ? /*#__PURE__*/_react["default"].createElement("span", {
        onClick: this.toggle,
        className: "".concat(prefixCls, "-zero-width-trigger")
      }, /*#__PURE__*/_react["default"].createElement(_icon["default"], {
        type: "bars"
      })) : null;
      var iconObj = {
        expanded: reverseArrow ? /*#__PURE__*/_react["default"].createElement(_icon["default"], {
          type: "right"
        }) : /*#__PURE__*/_react["default"].createElement(_icon["default"], {
          type: "left"
        }),
        collapsed: reverseArrow ? /*#__PURE__*/_react["default"].createElement(_icon["default"], {
          type: "left"
        }) : /*#__PURE__*/_react["default"].createElement(_icon["default"], {
          type: "right"
        })
      };
      var status = collapsed ? 'collapsed' : 'expanded';
      var defaultTrigger = iconObj[status];
      var triggerDom = trigger !== null ? zeroWidthTrigger || /*#__PURE__*/_react["default"].createElement("div", {
        className: "".concat(prefixCls, "-trigger"),
        onClick: this.toggle,
        style: {
          width: siderWidth
        }
      }, trigger || defaultTrigger) : null;
      var divStyle = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, style), {}, {
        flex: "0 0 ".concat(siderWidth),
        maxWidth: siderWidth,
        minWidth: siderWidth,
        width: siderWidth
      });
      var siderCls = (0, _classnames["default"])(className, prefixCls, "".concat(prefixCls, "-").concat(theme), (_classNames = {}, (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-collapsed"), !!collapsed), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-has-trigger"), collapsible && trigger !== null && !zeroWidthTrigger), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-below"), !!below), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-zero-width"), parseFloat(siderWidth) === 0), _classNames));
      return /*#__PURE__*/_react["default"].createElement(_LayoutSiderContext.LayoutSiderContextProvider, (0, _extends2["default"])({}, this.getContextValue(), {
        getPrefixCls: getPrefixCls
      }), /*#__PURE__*/_react["default"].createElement("div", (0, _extends2["default"])({
        className: siderCls
      }, divProps, {
        style: divStyle
      }), /*#__PURE__*/_react["default"].createElement("div", {
        className: "".concat(prefixCls, "-children")
      }, children), collapsible || below && zeroWidthTrigger ? triggerDom : null));
    }
  }], [{
    key: "contextType",
    get: function get() {
      return _LayoutContext["default"];
    }
  }]);
  return Sider;
}(_react.PureComponent);

exports["default"] = Sider;
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
