"use strict";

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

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

var _tslib = require("tslib");

var _consoleComponents = require("@alicloud/console-components");

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

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

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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 LPCascaderSelect =
/** @class */
function (_super) {
  (0, _tslib.__extends)(LPCascaderSelect, _super);

  function LPCascaderSelect(props) {
    var _this = _super.call(this, props) || this;

    _this.componentWillReceiveProps = function (nextProps) {
      _this.setState({
        dataSource: nextProps.dataSource
      });
    };

    _this.handleChange = function (value, data, extra) {
      _this.props.handleChange && _this.props.handleChange(value, data, extra);
    };

    _this.addDataSource = function (data, targetValue, needAddData) {
      for (var _i = 0, data_1 = data; _i < data_1.length; _i++) {
        var one = data_1[_i];

        if (one.value === targetValue) {
          if (!one.children) {
            one['children'] = [];
          }

          one['children'] = needAddData;
          return true;
        } else if (one.children && one.children.length > 0) {
          if (_this.addDataSource(one.children, targetValue, needAddData)) {
            return;
          }
        }
      }

      return;
    };

    _this.handleLoadData = function (data) {
      if (data.children && data.children.length > 0) {
        //说明是有数据节点不做处理
        return new Promise(function (resolve) {
          resolve();
        });
      } else if (_this.props.fetchData) {
        return _this.props.fetchData(data).then(function (result) {
          if (result.length > 0) {
            var tempData = _this.state.dataSource.splice(0);

            _this.addDataSource(tempData, data.value, result);

            _this.setState({
              dataSource: tempData
            });
          } else {
            _consoleComponents.Message.notice('抱歉没有数据');
          }
        });
      } else {
        return new Promise(function (resolve) {
          resolve();
        });
      }
    };

    _this.state = {
      dataSource: _this.props.dataSource ? _this.props.dataSource : []
    };
    return _this;
  }

  LPCascaderSelect.prototype.render = function () {
    var _a = this.props,
        width = _a.width,
        className = _a.className,
        triggerType = _a.triggerType,
        loadEmtyTip = _a.loadEmtyTip,
        handleChange = _a.handleChange,
        fetchData = _a.fetchData,
        dataSource = _a.dataSource,
        rest = (0, _tslib.__rest)(_a, ["width", "className", "triggerType", "loadEmtyTip", "handleChange", "fetchData", "dataSource"]);
    var style = {};

    if (width) {
      style = {
        width: width
      };
    }

    return /*#__PURE__*/_react["default"].createElement(_consoleComponents.CascaderSelect, (0, _tslib.__assign)({
      showSearch: true,
      className: (0, _classnames["default"])('lp-cascader-select', className),
      expandTriggerType: triggerType,
      dataSource: this.state.dataSource,
      onChange: this.handleChange,
      loadData: fetchData ? this.handleLoadData : undefined,
      popupClassName: "lp-cascader-select-over",
      listClassName: "lp-list-Item",
      style: style,
      popupStyle: style
    }, rest));
  };

  return LPCascaderSelect;
}(_react.Component);

var _default = LPCascaderSelect;
exports["default"] = _default;
LPCascaderSelect.defaultProps = {
  showSearch: true,
  triggerType: 'click',
  loadEmtyTip: '抱歉没有数据'
};