"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

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

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

var _antd = require("antd");

var _mobx = require("../../mobx");

var _RcCollapse = _interopRequireWildcard(require("../RcCollapse"));

var _Iconfont = _interopRequireDefault(require("../Iconfont"));

require("./index.less");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

var __extends = void 0 && (void 0).__extends || function () {
  var _extendStatics = function extendStatics(d, b) {
    _extendStatics = Object.setPrototypeOf || {
      __proto__: []
    } instanceof Array && function (d, b) {
      d.__proto__ = b;
    } || function (d, b) {
      for (var p in b) {
        if (b.hasOwnProperty(p)) d[p] = b[p];
      }
    };

    return _extendStatics(d, b);
  };

  return function (d, b) {
    _extendStatics(d, b);

    function __() {
      this.constructor = d;
    }

    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  };
}();

var __assign = void 0 && (void 0).__assign || function () {
  __assign = Object.assign || function (t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];

      for (var p in s) {
        if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
      }
    }

    return t;
  };

  return __assign.apply(this, arguments);
};

var debug = require('debug')('worker:Collapse'); // @inject('store')
// @observer


var CommonCollapse =
/** @class */
function (_super) {
  __extends(CommonCollapse, _super);

  function CommonCollapse() {
    var _this = _super !== null && _super.apply(this, arguments) || this;

    _this.state = {
      activeKey: ['1']
    }; // 初始化展开面板

    _this.initActive = function (props) {
      var _a, _b;

      var actives = [];
      var type = props.type,
          source = props.source;

      if (source.length === 0) {
        return;
      }

      if (type === 'switch') {
        debug('source', source);
        var first = source.find(function (src) {
          var _a;

          return (_a = src.metaOption) === null || _a === void 0 ? void 0 : _a.open;
        });

        if (first && !_mobx.store.clickedMeta) {
          _mobx.store.setState({
            clickedMeta: (_a = first.metaOption) === null || _a === void 0 ? void 0 : _a.id
          });

          actives.push(((_b = first.metaOption) === null || _b === void 0 ? void 0 : _b.id) || first.key);
        }
      } else {
        var first = source.find(function (src) {
          var _a;

          return ((_a = src.metaOption) === null || _a === void 0 ? void 0 : _a.open) !== false;
        });
        actives.push(first === null || first === void 0 ? void 0 : first.key);
      }

      _this.setState({
        activeKey: actives
      });
    };

    _this.onCheckBoxChange = function (e, panelInfo) {
      var _a;

      var checked = e.target.checked;
      panelInfo.metaOption.open = checked;
      var activeKey = _this.state.activeKey;

      if (checked) {
        _this.setState({
          activeKey: [((_a = panelInfo.metaOption) === null || _a === void 0 ? void 0 : _a.id) || panelInfo.key]
        });
      } else {
        _this.setState({
          activeKey: activeKey.filter(function (key) {
            return key !== panelInfo.key;
          })
        });
      }

      _this.props.onRefresh();
    };

    _this.onSwitchChange = function (checked, panelInfo) {
      var _a;

      panelInfo.metaOption.open = checked;

      if (checked) {
        _this.setState({
          activeKey: [((_a = panelInfo.metaOption) === null || _a === void 0 ? void 0 : _a.id) || panelInfo.key]
        });
      } else {
        _this.setState({
          activeKey: []
        });
      }

      _this.props.onRefresh();
    };

    _this.getExtraInfo = function (panel) {
      var _a;

      var activeKey = _this.state.activeKey;
      var type = _this.props.type;
      var toggleType = panel.toggleType,
          key = panel.key;
      var extraType = toggleType || type; // 三角类型折叠

      if (extraType === 'triangle') {
        if (activeKey.includes(key)) {
          return /*#__PURE__*/_react["default"].createElement(_Iconfont["default"], {
            color: "#8493AF",
            size: 20,
            type: "icon-up"
          });
        }

        return /*#__PURE__*/_react["default"].createElement(_Iconfont["default"], {
          color: "#8493AF",
          size: 20,
          type: "icon-down"
        });
      } // 箭头类型折叠


      if (extraType === 'arrow') {
        if (activeKey.includes(key)) {
          return /*#__PURE__*/_react["default"].createElement(_Iconfont["default"], {
            color: "#8493AF",
            size: 20,
            type: "icon-outlined-up"
          });
        }

        return /*#__PURE__*/_react["default"].createElement(_Iconfont["default"], {
          color: "#8493AF",
          size: 20,
          type: "icon-outlined-down"
        });
      } // switch类型折叠


      if (extraType === 'switch') {
        return /*#__PURE__*/_react["default"].createElement(_antd.Switch, {
          checked: (_a = panel.metaOption) === null || _a === void 0 ? void 0 : _a.open,
          onChange: function onChange(checked) {
            return _this.onSwitchChange(checked, panel);
          }
        });
      } // checkbox类型折叠


      if (extraType === 'checkbox') {
        return /*#__PURE__*/_react["default"].createElement(_antd.Checkbox, {
          checked: panel.metaOption.open,
          onChange: function onChange(e) {
            return _this.onCheckBoxChange(e, panel);
          }
        });
      }
    };

    _this.headerMouseEnter = function (panel) {
      var _a;

      if (_this.props.type !== 'switch') {
        return;
      }

      _mobx.store.setState({
        hoveredMeta: (_a = panel.metaOption) === null || _a === void 0 ? void 0 : _a.id
      });
    };

    _this.headerMouseLeave = function () {
      if (_this.props.type !== 'switch') {
        return;
      }

      _mobx.store.setState({
        hoveredMeta: ''
      });
    };

    _this.getHeader = function (panel) {
      var activeKey = _this.state.activeKey;
      var name = panel.name,
          key = panel.key;
      var _a = _this.props,
          headerStyle = _a.headerStyle,
          showActiveColor = _a.showActiveColor;
      var showColor = activeKey.includes(key) && showActiveColor;
      var activeStyle = showColor ? {
        color: '#2F54EB'
      } : {};
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: "header_wrap",
        onMouseEnter: function onMouseEnter() {
          return _this.headerMouseEnter(panel);
        },
        onMouseLeave: _this.headerMouseLeave,
        style: __assign(__assign({}, headerStyle), activeStyle)
      }, /*#__PURE__*/_react["default"].createElement("div", null, name));
    };

    _this.panelCollapsible = function (panel) {
      var type = _this.props.type;
      var toggleType = panel.toggleType,
          metaOption = panel.metaOption;
      var extraType = toggleType || type;

      if (['triangle', 'arrow'].includes(extraType)) {
        return null;
      } else {
        // switch 和checkbox模式只有在开启的状态下才能展开
        if (metaOption === null || metaOption === void 0 ? void 0 : metaOption.open) {
          return 'header';
        }

        return 'disabled';
      }
    };

    _this.showHeaderHover = function (panel) {
      var _a;

      var type = _this.props.type;

      if (_mobx.store.hoveredMeta === ((_a = panel.metaOption) === null || _a === void 0 ? void 0 : _a.id) && type === 'switch') {
        return true;
      }

      return false;
    };

    _this.onCollapseChange = function (value) {
      var type = _this.props.type;

      _this.setState({
        activeKey: value ? [value] : []
      });

      if (type === 'switch') {
        _mobx.store.setState({
          clickedMeta: value
        });
      }
    };

    return _this;
  }

  CommonCollapse.prototype.componentWillMount = function () {
    this.initActive(this.props);
  };

  CommonCollapse.prototype.componentDidMount = function () {
    var _this = this;

    this.distroy = (0, _mobx.autorun)(function () {
      if (_this.props.type === 'switch' && _mobx.store.clickedMeta) {
        _this.setState({
          activeKey: [_mobx.store.clickedMeta]
        });
      }
    });
  }; // componentWillReceiveProps(nextProps) {
  //   console.log(this.props);
  //   console.log(nextProps);
  //   this.initActive(nextProps);
  // }


  CommonCollapse.prototype.componentWillUnmount = function () {
    this.distroy();
  };

  CommonCollapse.prototype.render = function () {
    var _this = this;

    var _a = this.props,
        source = _a.source,
        headerSize = _a.headerSize,
        divider = _a.divider;
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: "collapse_wrap " + headerSize + "_header " + divider + "_divider"
    }, /*#__PURE__*/_react["default"].createElement(_RcCollapse["default"], {
      accordion: true,
      activeKey: this.state.activeKey // destroyInactivePanel
      ,
      onChange: this.onCollapseChange
    }, source.map(function (panel) {
      var _a;

      return /*#__PURE__*/_react["default"].createElement(_RcCollapse.Panel, {
        key: ((_a = panel.metaOption) === null || _a === void 0 ? void 0 : _a.id) || panel.key,
        collapsible: _this.panelCollapsible(panel),
        extra: _this.getExtraInfo(panel),
        header: _this.getHeader(panel),
        headerClass: _this.showHeaderHover(panel) ? 'rc-collapse-header-hover' : '',
        showArrow: false
      }, panel.value);
    })));
  };

  CommonCollapse.defaultProps = {
    type: 'triangle',
    source: [],
    headerStyle: {},
    divider: 'solid',
    headerSize: 'default',
    onRefresh: function onRefresh() {}
  };
  return CommonCollapse;
}(_react["default"].Component);

var _default = CommonCollapse;
exports["default"] = _default;