/* eslint-disable */
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
var _excluded = ["columns"];
import _regeneratorRuntime from "@babel/runtime/regenerator";
import { falcon as _falcon } from "@mas.io/falcon-core";
import { MasComponent } from './typings/mas';
import { getTreeMaxFloor, guid2, deepClone, isEqual } from './utils/util';

var Index = /*#__PURE__*/function (_MasComponent) {
  _inheritsLoose(Index, _MasComponent);

  function Index() {
    var _this;

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _MasComponent.call.apply(_MasComponent, [this].concat(args)) || this;
    _this.props = {
      columns: [],
      title: '标题',
      itemHeight: 44,
      defaultIndex: 0,
      valueKey: 'text',
      showToolbar: false,
      visibleItemCount: 5,
      cancelButtonText: '取消',
      confirmButtonText: '确认',
      onCancel: function onCancel(arr) {
        return arr;
      },
      onChange: function onChange(arr) {
        return arr;
      },
      onConfirm: function onConfirm(arr) {
        return arr;
      },
      toolbarPosition: 'top'
    };
    _this.data = {
      maxFloor: 0,
      columnsArr: [],
      componentUuid: ''
    };
    return _this;
  }

  var _proto = Index.prototype;

  _proto.onInit = function onInit() {
    /* 生成 uuid */
    var uuid = guid2();
    if (!this.$page[uuid]) this.$page[uuid] = {};
    this.setData({
      componentUuid: uuid
    });
  };

  _proto.didMount = /*#__PURE__*/function () {
    var _didMount = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
      return _regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.next = 2;
              return this.updateStateByProps(this.props);

            case 2:
            case "end":
              return _context.stop();
          }
        }
      }, _callee, this);
    }));

    function didMount() {
      return _didMount.apply(this, arguments);
    }

    return didMount;
  }();

  _proto.updateStateByProps = /*#__PURE__*/function () {
    var _updateStateByProps = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(props) {
      var curColumns, rest, columns, _columns$0$values, values, maxFloor, defaultIndex, columnsIsDifferent;

      return _regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              curColumns = props.columns, rest = _objectWithoutPropertiesLoose(props, _excluded); // eslint-disable-next-line no-param-reassign

              columns = deepClone(curColumns);
              /* 数据兼容 */

              if (!columns[0].values) {
                // eslint-disable-next-line no-param-reassign
                columns = [{
                  values: columns
                }];
              }

              _columns$0$values = columns[0].values, values = _columns$0$values === void 0 ? [] : _columns$0$values;
              maxFloor = getTreeMaxFloor(values);
              /* 如果层级大于1，则走级联逻辑 */

              if (!(maxFloor > 1)) {
                _context2.next = 9;
                break;
              }

              _context2.next = 8;
              return this.setColumns(values, maxFloor);

            case 8:
              return _context2.abrupt("return");

            case 9:
              defaultIndex = rest.defaultIndex;
              /* 这里是普通单级 */

              _context2.next = 12;
              return this.setData({
                maxFloor: columns.length,
                columnsArr: this.setDefaultIndex(columns, defaultIndex)
              });

            case 12:
              columnsIsDifferent = JSON.stringify(this.data.columnsArr) === JSON.stringify(this.setDefaultIndex(columns, defaultIndex));

              if (!columnsIsDifferent) {
                this.changeHandle();
              }

            case 14:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2, this);
    }));

    function updateStateByProps(_x) {
      return _updateStateByProps.apply(this, arguments);
    }

    return updateStateByProps;
  }();

  _proto.didUpdate = /*#__PURE__*/function () {
    var _didUpdate = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(preProps) {
      var preColumns, curColumns;
      return _regeneratorRuntime.wrap(function _callee3$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              preColumns = preProps.columns;
              curColumns = this.props.columns; // 比较两次的数据是否一致

              if (!isEqual(preColumns, curColumns)) {
                _context3.next = 4;
                break;
              }

              return _context3.abrupt("return");

            case 4:
              _context3.next = 6;
              return this.updateStateByProps(this.props);

            case 6:
            case "end":
              return _context3.stop();
          }
        }
      }, _callee3, this);
    }));

    function didUpdate(_x2) {
      return _didUpdate.apply(this, arguments);
    }

    return didUpdate;
  }()
  /* 设置默认选择 */
  ;

  _proto.setDefaultIndex = function setDefaultIndex(cols, index) {
    var indexes = [].concat(index);
    cols.forEach(function (col, i) {
      if (col.defaultIndex === undefined) {
        var defaultIndex = indexes[i];

        if (defaultIndex !== undefined) {
          // eslint-disable-next-line no-param-reassign
          col.defaultIndex = defaultIndex;
        }
      }
    });
    return cols;
  };

  _proto.noop = function noop() {// todo
  };

  _proto.pageCache = function pageCache() {
    var componentUuid = this.data.componentUuid;
    return this.$page[componentUuid] || {};
  };

  _proto.getCurrentValue = function getCurrentValue() {
    var componentUuid = this.data.componentUuid;
    return this[componentUuid + "_curValue"];
  };

  _proto.setCurrentValue = function setCurrentValue(value) {
    var componentUuid = this.data.componentUuid;
    this[componentUuid + "_curValue"] = value;
  };

  _proto.changeHandle = function changeHandle(index, ind, item) {
    var _this$props;

    var columnsArr = this.data.columnsArr;
    /* 如果item为字符串则不是级联选择 */

    if (item && typeof item !== 'string') {
      this.updateValues(index, ind, item);
    }

    var arr = [];
    var pageCache = this.pageCache();

    for (var i = 0; i < columnsArr.length; i++) {
      var val = pageCache["column" + i].getValue();
      if (val) arr.push(val);
    }

    this.setCurrentValue(arr);
    (_this$props = this.props) == null ? void 0 : _this$props.onChange == null ? void 0 : _this$props.onChange(arr);
  };

  _proto.createIndexArr = function createIndexArr(length) {
    return Array.from({
      length: length
    }).fill(0);
  };

  _proto.updateValues = function updateValues(index, ind, item) {
    var _this$data = this.data,
        maxFloor = _this$data.maxFloor,
        _this$data$columnsArr = _this$data.columnsArr,
        columnsArr = _this$data$columnsArr === void 0 ? [] : _this$data$columnsArr;
    var pageCache = this.pageCache();
    if (ind === columnsArr.length - 1) return;

    var _ref = item || {},
        _ref$children = _ref.children,
        children = _ref$children === void 0 ? [] : _ref$children;

    var floor = getTreeMaxFloor(children);
    var indexArr = this.createIndexArr(floor);
    var arr = this.getRenderList(children, indexArr);

    for (var i = ind + 1; i < maxFloor; i++) {
      var _ref2 = arr[i - ind - 1] || {},
          _ref2$values = _ref2.values,
          values = _ref2$values === void 0 ? [] : _ref2$values;

      pageCache["column" + i].setColumns(values, 0);
    }
  };

  _proto.setColumns = /*#__PURE__*/function () {
    var _setColumns = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(columns, maxFloor) {
      var defaultIndex, indexArr, indexes, i, arr;
      return _regeneratorRuntime.wrap(function _callee4$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              defaultIndex = this.props.defaultIndex;
              indexArr = this.createIndexArr(maxFloor);
              indexes = [].concat(defaultIndex);

              if (indexes.length) {
                for (i = 0; i < indexArr.length; i++) {
                  if (defaultIndex[i] !== undefined) {
                    indexArr[i] = defaultIndex[i];
                  }
                }
              }

              arr = this.getRenderList(columns, indexArr);
              _context4.next = 7;
              return this.setData({
                maxFloor: maxFloor,
                columnsArr: arr
              });

            case 7:
              this.changeHandle();

            case 8:
            case "end":
              return _context4.stop();
          }
        }
      }, _callee4, this);
    }));

    function setColumns(_x3, _x4) {
      return _setColumns.apply(this, arguments);
    }

    return setColumns;
  }();

  _proto.setValues = function setValues(arr, indexArr, newArr, index) {
    if (index === void 0) {
      index = 0;
    }

    var cache = {
      values: [],
      defaultIndex: indexArr[index]
    };
    var curIndex = indexArr[index];
    var item = arr[curIndex] || {};
    arr.forEach(function (_item) {
      cache.values.push(_item);
    });
    var _item$children = item.children,
        children = _item$children === void 0 ? [] : _item$children;

    if (children.length) {
      var nextIndex = index + 1;
      this.setValues(children, indexArr, newArr, nextIndex);
    }

    newArr.unshift(cache);
  };

  _proto.getRenderList = function getRenderList(arr, indexArr) {
    var newArr = [];
    var defaultItem = {
      values: [],
      defaultIndex: 0
    };
    this.setValues(arr, indexArr, newArr);

    if (newArr.length !== indexArr.length) {
      var diffLen = indexArr.length - newArr.length;
      newArr.push.apply(newArr, Array.from({
        length: diffLen
      }).fill(defaultItem));
    }

    return newArr;
  };

  _proto.confirmHandel = function confirmHandel() {
    var _this$props2;

    (_this$props2 = this.props) == null ? void 0 : _this$props2.onConfirm(this.getCurrentValue());
  };

  _proto.cancelHandel = function cancelHandel() {
    var _this$props3;

    (_this$props3 = this.props) == null ? void 0 : _this$props3.onCancel(this.getCurrentValue());
  };

  return Index;
}(MasComponent);

export { Index as default };
var instance = new Index();

_falcon.launch(instance, "Component", {
  componentName: "@alipay/adc-picker"
});

Component(instance);