"use strict";

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

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

var _antd = require("antd");

require("./index.less");

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 (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p];
      }
    };

    return _extendStatics(d, b);
  };

  return function (d, b) {
    if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");

    _extendStatics(d, b);

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

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

var __spreadArray = void 0 && (void 0).__spreadArray || function (to, from, pack) {
  if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
    if (ar || !(i in from)) {
      if (!ar) ar = Array.prototype.slice.call(from, 0, i);
      ar[i] = from[i];
    }
  }
  return to.concat(ar || Array.prototype.slice.call(from));
};

var Panel = _antd.Collapse.Panel;

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

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

    _this.state = {
      activeKey: []
    }; // switch 模式展开第一个open状态，checkbox模式展开所有open状态

    _this.openSwitch = function (source) {
      var type = _this.props.type;
      var activeSource = [];

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

      if (type === 'checkout') {
        activeSource = source.filter(function (src) {
          return src.metaOption.open;
        }).map(function (src) {
          return src.key;
        });
      } else {
        var openMeta = source.find(function (meta) {
          return meta.metaOption.open;
        });

        if (openMeta) {
          activeSource.push(openMeta.key);
        }
      }

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

    _this.onSwitchChange = function (checked, panelInfo) {
      panelInfo.metaOption.open = checked;

      if (checked) {
        _this.setState({
          activeKey: panelInfo.key
        });
      } else {
        _this.setState({
          activeKey: null
        });
      }

      _this.props.onRefresh();
    };

    _this.onCheckBoxChange = function (e, panelInfo) {
      var checked = e.target.checked;
      panelInfo.metaOption.open = checked;
      var activeKey = _this.state.activeKey;

      if (checked) {
        _this.setState({
          activeKey: activeKey ? __spreadArray(__spreadArray([], activeKey, true), [panelInfo.key], false) : [panelInfo.key]
        });
      } else {
        _this.setState({
          activeKey: activeKey.find(function (key) {
            return key !== panelInfo.key;
          })
        });
      }

      _this.props.onRefresh();
    };

    _this.getSwitchButton = function (panelInfo) {
      var type = _this.props.type;

      if (type === 'switch') {
        return /*#__PURE__*/_react["default"].createElement(_antd.Switch, {
          checked: panelInfo.metaOption.open,
          onChange: function onChange(checked) {
            return _this.onSwitchChange(checked, panelInfo);
          },
          size: "small"
        });
      } else {
        return /*#__PURE__*/_react["default"].createElement(_antd.Checkbox, {
          checked: panelInfo.metaOption.open,
          onChange: function onChange(e) {
            return _this.onCheckBoxChange(e, panelInfo);
          }
        });
      }
    };

    _this.getHeader = function (name, index) {
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: "header_wrap"
      }, index !== 0 && /*#__PURE__*/_react["default"].createElement("div", {
        className: "meta_header_divider"
      }), /*#__PURE__*/_react["default"].createElement("span", {
        className: "switchcollapse_header_name"
      }, name));
    };

    return _this;
  }

  SwitchCollapse.prototype.componentWillMount = function () {
    this.openSwitch(this.props.source);
  };

  SwitchCollapse.prototype.componentWillReceiveProps = function (nextProps) {
    this.openSwitch(nextProps.source);
  };

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

    var _a = this.props,
        source = _a.source,
        accordion = _a.accordion,
        type = _a.type;
    var activeKey = this.state.activeKey;
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: "".concat(type, "_collapse_wrap")
    }, /*#__PURE__*/_react["default"].createElement(_antd.Collapse, {
      accordion: accordion,
      activeKey: activeKey,
      ghost: true,
      onChange: function onChange(value) {
        return _this.setState({
          activeKey: value
        });
      }
    }, source.map(function (panelInfo, index) {
      return /*#__PURE__*/_react["default"].createElement(Panel, {
        key: panelInfo.key,
        collapsible: panelInfo.metaOption.open ? 'header' : 'disabled',
        extra: _this.getSwitchButton(panelInfo),
        header: _this.getHeader(panelInfo.name, index),
        showArrow: false
      }, panelInfo.value);
    })));
  };

  SwitchCollapse.defaultProps = {
    source: [],
    accordion: true,
    type: 'switch'
  };
  return SwitchCollapse;
}(_react["default"].Component);

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