/* eslint-disable */
import _extends from "@babel/runtime/helpers/extends";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import { falcon as _falcon } from "@mas.io/falcon-core";
import { MasComponent } from '../typings/mas';
import { isObj, range, 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 = {
      index: 0,
      columnIndex: 'column0',
      valueKey: '',
      defaultIndex: 0,
      itemHeight: 44,
      componentUuid: '',
      visibleItemCount: 3,
      // eslint-disable-next-line no-unused-vars
      onChange: function onChange(index, ind, item) {},
      columnList: []
    };
    _this.data = {
      startY: 0,
      offset: 0,
      duration: 0,
      startOffset: 0,
      options: [],
      currentIndex: 0
    };
    return _this;
  }

  var _proto = Index.prototype;

  _proto.onInit = /*#__PURE__*/function () {
    var _onInit = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
      var _this$props, columnList, columnIndex, defaultIndex, uuid, pageCache;

      return _regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _this$props = this.props, columnList = _this$props.columnList, columnIndex = _this$props.columnIndex, defaultIndex = _this$props.defaultIndex, uuid = _this$props.componentUuid;
              pageCache = this.$page[uuid];
              pageCache[columnIndex] = this;
              _context.next = 5;
              return this.setData({
                options: columnList,
                currentIndex: defaultIndex
              });

            case 5:
              this.setIndex(defaultIndex);

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

    function onInit() {
      return _onInit.apply(this, arguments);
    }

    return onInit;
  }() // eslint-disable-next-line no-unused-vars
  ;

  _proto.didUpdate = /*#__PURE__*/function () {
    var _didUpdate = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(_, preState) {
      var obj, preCurIndex, _this$data, _this$data$options, options, currentIndex, _this$props2, _this$props2$defaultI, curIndex, _this$props2$columnLi, curColumnList;

      return _regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              obj = {};
              preCurIndex = preState.currentIndex;
              _this$data = this.data, _this$data$options = _this$data.options, options = _this$data$options === void 0 ? [] : _this$data$options, currentIndex = _this$data.currentIndex;
              _this$props2 = this.props, _this$props2$defaultI = _this$props2.defaultIndex, curIndex = _this$props2$defaultI === void 0 ? 0 : _this$props2$defaultI, _this$props2$columnLi = _this$props2.columnList, curColumnList = _this$props2$columnLi === void 0 ? [] : _this$props2$columnLi; // 判断index 是否发生变化

              if (preCurIndex === currentIndex) {
                if (currentIndex !== curIndex) {
                  obj.currentIndex = curIndex;
                }
              } else {
                obj.currentIndex = currentIndex;
              } // 判断数据是否发生变化


              if (!isEqual(options, curColumnList)) {
                obj.options = curColumnList;
              }

              if (!(Object.keys(obj).length > 0)) {
                _context2.next = 10;
                break;
              }

              _context2.next = 9;
              return this.setData(_extends({}, obj));

            case 9:
              this.setIndex(this.data.currentIndex, true);

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

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

    return didUpdate;
  }();

  _proto.getCount = function getCount() {
    return this.data.options.length;
  };

  _proto.touchStart = function touchStart(event) {
    this.setData({
      startY: event.touches[0].clientY,
      startOffset: this.data.offset,
      duration: 0
    });
  };

  _proto.touchMove = function touchMove(e) {
    var deltaY = e.touches[0].clientY - this.data.startY;
    this.setData({
      offset: range(this.data.startOffset + deltaY, -(this.getCount() * this.props.itemHeight), this.props.itemHeight)
    });
  };

  _proto.touchEnd = function touchEnd() {
    var data = this.data;

    if (data.offset !== data.startOffset) {
      this.setData({
        duration: 200
      });
      var index = range(Math.round(-data.offset / this.props.itemHeight), 0, this.getCount() - 1);
      this.setIndex(index, true);
    }
  };

  _proto.setIndex = function setIndex(ind, userAction) {
    var _this2 = this;

    var index = this.adjustIndex(ind) || 0;
    var offset = -index * this.props.itemHeight;

    if (offset !== this.data.offset) {
      return this.setData({
        offset: offset,
        currentIndex: index
      }, function () {
        var _this2$data$options;

        var currentItem = (_this2$data$options = _this2.data.options) == null ? void 0 : _this2$data$options[index];
        userAction && _this2.props.onChange(index, _this2.props.index, currentItem);
      });
    }

    return this.setData({
      offset: offset
    });
  };

  _proto.adjustIndex = function adjustIndex(ind) {
    var data = this.data;
    var count = this.getCount();
    var index = range(ind, 0, count);

    for (var i = index; i < count; i++) {
      if (!this.isDisabled(data.options[i])) {
        return i;
      }
    }

    for (var _i = index - 1; _i >= 0; _i--) {
      if (!this.isDisabled(data.options[_i])) {
        return _i;
      }
    }

    return 0;
  };

  _proto.isDisabled = function isDisabled(option) {
    return isObj(option) && option.disabled;
  };

  _proto.onClickItem = function onClickItem(event) {
    var index = event.currentTarget.dataset.index;
    this.setIndex(+index, true);
    this.setData({
      duration: 300
    });
  };

  _proto.setValue = function setValue(value) {
    var options = this.data.options;

    for (var i = 0; i < options.length; i++) {
      if (this.getOptionText(options[i]) === value) {
        return this.setIndex(i);
      }
    }

    return Promise.resolve();
  };

  _proto.getValue = function getValue() {
    var _this$data2 = this.data,
        options = _this$data2.options,
        currentIndex = _this$data2.currentIndex;
    if (!options.length) return null;
    return {
      value: options[currentIndex],
      index: currentIndex
    };
  };

  _proto.getOptionText = function getOptionText(option) {
    return isObj(option) && this.props.valueKey in option ? option[this.props.valueKey] : option;
  };

  _proto.setColumns = function setColumns(value, index) {
    var _this3 = this;

    this.setData({
      options: value
    }, function () {
      _this3.setIndex(index, true);
    });
  };

  return Index;
}(MasComponent);

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

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

Component(instance);