/*! bxs-vue-ui v3.3.0 */
'use strict';

var entryVirtual = require('./entry-virtual.js');
var normalizeComponent = require('./normalize-component.js');
require('./es.number.constructor.js');
var forEach = require('./for-each.js');
var map = require('./map.js');
var indexOf = require('./index-of.js');
var splice = require('./splice.js');
var stringify = require('./stringify.js');

var values = entryVirtual.entryVirtual('Array').values;

var values$1 = values;

var ArrayPrototype = Array.prototype;

var DOMIterables = {
  DOMTokenList: true,
  NodeList: true
};

var values_1 = function (it) {
  var own = it.values;
  return it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.values)
    // eslint-disable-next-line no-prototype-builtins
    || DOMIterables.hasOwnProperty(normalizeComponent.classof$1(it)) ? values$1 : own;
};

var values$2 = values_1;

//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
function range(num, min, max) {
  return Math.min(Math.max(num, min), max);
}

var DEFAULT_DURATION = 200;
var script = {
  name: 'picker-item',
  props: {
    values: {
      type: Array,
      default: function _default() {
        return [];
      }
    },
    defaultIndex: {
      type: Number,
      default: 0
    },
    itemCount: {
      type: Number,
      default: 7
    },
    valueKey: {
      type: String,
      default: 'text'
    },
    className: {
      type: String,
      default: ''
    }
  },
  components: {},
  data: function data() {
    return {
      pickerTranslateY: 0,
      pickerState: {
        data: values$2(this),
        selectedId: null,
        index: 0,
        startPos: null,
        translateY: 0,
        startTranslateY: 0,
        dragging: true
      },
      options: JSON.parse(stringify._JSON$stringify(values$2(this))),
      currentIndex: this.defaultIndex,
      // 当前选择项
      startY: 0,
      offset: 0,
      duration: 0,
      startOffset: 0,
      isTouchInline: false,
      itemHeight: 30,
      itemSelectHeight: 40
    };
  },
  watch: {
    defaultIndex: function defaultIndex() {
      this.setOptionIndex(this.defaultIndex);
    },
    values: function values(n) {
      if (n) {
        this.options = JSON.parse(stringify._JSON$stringify(n));
      }
    }
  },
  computed: {
    pickerStyle: function pickerStyle() {
      return {
        height: this.itemHeight * (this.itemCount - 1) + this.itemSelectHeight + "px"
      };
    },
    count: function count() {
      return this.options.length;
    },
    baseOffset: function baseOffset() {
      return this.itemHeight * (this.itemCount - 1) / 2;
    },
    wrapperStyle: function wrapperStyle() {
      return {
        transition: this.duration + "ms",
        '-webkit-transition': this.duration + "ms",
        '-moz-transition': this.duration + "ms",
        '-o-transition': this.duration + "ms",
        transform: "translate3d(0, " + (this.offset + this.baseOffset) + "px, 0)",
        '-ms-transform': "translate3d(0, " + (this.offset + this.baseOffset) + "px, 0)",
        '-webkit-transform': "translate3d(0, " + (this.offset + this.baseOffset) + "px, 0)",
        '-o-transform': "translate3d(0, " + (this.offset + this.baseOffset) + "px, 0)",
        '-moz-transform': "translate3d(0, " + (this.offset + this.baseOffset) + "px, 0)",
        lineHeight: this.itemHeight + "px"
      };
    }
  },
  methods: {
    onTouchStart: function onTouchStart(event) {
      this.startY = event.touches[0].clientY;
      this.startOffset = this.offset;
      this.duration = 0;
      this.isTouchInline = false;
    },
    onTouchMove: function onTouchMove(event) {
      var deltaY = event.touches[0].clientY - this.startY;
      this.isTouchInline = true;
      this.offset = range(this.startOffset + deltaY, -((this.count - 1) * this.itemHeight + this.itemSelectHeight), this.itemHeight);
    },
    onTouchEnd: function onTouchEnd() {
      if (this.offset !== this.startOffset) {
        this.duration = DEFAULT_DURATION;
        var index = range(Math.round(-this.offset / this.itemHeight), 0, this.count - 1);
        this.setOptionIndex(index, true);
      }

      this.isTouchInline = false;
    },
    isDisabled: function isDisabled(option) {
      return this.isObject(option) && option.disabled;
    },
    isObject: function isObject(value) {
      var type = typeof value;
      return value != null && (type === 'object' || type === 'function');
    },
    setOptionIndex: function setOptionIndex(index, userAction) {
      if (index < this.options.length) {
        if (index !== this.currentIndex) {
          this.currentIndex = index;

          if (userAction) {
            this.$emit('change', index);
          }
        }
      } else if (index !== this.currentIndex) {
        this.currentIndex = this.options.length - 1;

        if (userAction) {
          this.$emit('change', this.currentIndex);
        }
      }

      this.offset = -this.currentIndex * this.itemHeight;
    },
    getOptionText: function getOptionText(option) {
      return this.isObject(option) && this.valueKey in option ? option[this.valueKey] : option;
    },
    setValue: function setValue(value) {
      var options = this.options;

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

      return this.setOptionIndex(0, true);
    },
    getValue: function getValue() {
      return this.options[this.currentIndex];
    }
  },
  created: function created() {
    if (this.$parent && this.$parent.children) {
      this.$parent.children.push(this);
    }
  },
  mounted: function mounted() {
    var _this = this;

    this.setOptionIndex(this.currentIndex);
    this.$nextTick(function () {
      if (_this.$refs.bxPickerLi) {
        _this.itemHeight = _this.$refs.bxPickerLi[0].offsetHeight;
      }

      if (_this.$refs.bxPickerLiSelect) {
        _this.itemSelectHeight = _this.$refs.bxPickerLiSelect[0].offsetHeight;
      }
    });
  },
  destroyed: function destroyed() {
    var children = this.$parent.children;

    if (children) {
      splice._spliceInstanceProperty(children).call(children, indexOf._indexOfInstanceProperty(children).call(children, this), 1);
    }
  }
};

/* script */
var __vue_script__ = script;
/* template */

var __vue_render__ = function __vue_render__() {
  var _vm = this;

  var _h = _vm.$createElement;

  var _c = _vm._self._c || _h;

  return _c('div', {
    class: ['bx-picker-well', _vm.className],
    style: _vm.pickerStyle,
    on: {
      "touchstart": _vm.onTouchStart,
      "touchmove": function touchmove($event) {
        $event.preventDefault();
        return _vm.onTouchMove($event);
      },
      "touchend": _vm.onTouchEnd,
      "touchcancel": _vm.onTouchEnd
    }
  }, [_c('ul', {
    ref: "bxPickerUl",
    staticClass: "bx-picker-scroll",
    style: _vm.wrapperStyle
  }, _vm._l(_vm.options, function (item, index) {
    return _c('li', {
      key: index,
      ref: index === _vm.currentIndex ? 'bxPickerLiSelect' : 'bxPickerLi',
      refInFor: true,
      class: ['bx-picker-item', {
        'disabled': _vm.isDisabled(item)
      }, {
        'selected': index === _vm.currentIndex
      }],
      style: {
        opacity: _vm.isTouchInline ? 0.5 : 1 - Math.abs(index - _vm.currentIndex) * 0.3
      },
      on: {
        "click": function click($event) {
          _vm.setOptionIndex(index, true);
        }
      }
    }, [_vm._v("\n      " + _vm._s(typeof item === 'object' && item[_vm.valueKey] ? item[_vm.valueKey] : item) + "\n    ")]);
  }))]);
};

var __vue_staticRenderFns__ = [];
/* style */

var __vue_inject_styles__ = undefined;
/* scoped */

var __vue_scope_id__ = undefined;
/* module identifier */

var __vue_module_identifier__ = undefined;
/* functional template */

var __vue_is_functional_template__ = false;
/* style inject */

/* style inject SSR */

var pickerItem = normalizeComponent.__vue_normalize__({
  render: __vue_render__,
  staticRenderFns: __vue_staticRenderFns__
}, __vue_inject_styles__, __vue_script__, __vue_scope_id__, __vue_is_functional_template__, __vue_module_identifier__, undefined, undefined);

var _components;
var script$1 = normalizeComponent.create({
  name: 'picker',
  props: {
    title: {
      // 选择展示标题
      type: String,
      default: ''
    },
    valueKey: {
      // 传递对象显示的值
      type: String,
      default: 'text'
    },
    pickerData: {
      type: Array,
      default: function _default() {
        return [];
      }
    },
    showToolbar: {
      type: Boolean,
      default: false
    },
    value: Array
  },
  components: (_components = {}, _components[pickerItem.name] = pickerItem, _components),
  data: function data() {
    return {
      children: []
    };
  },
  computed: {
    pickerDataType: function pickerDataType() {
      return !!(this.pickerData.length && !values$2(this.pickerData[0]));
    },
    pickerDataList: function pickerDataList() {
      return this.pickerData.length && !values$2(this.pickerData[0]) ? [this.pickerData] : this.pickerData;
    }
  },
  watch: {
    pickerData: function pickerData() {
      this.setColumns();
    }
  },
  methods: {
    setColumns: function setColumns() {
      var _this = this;

      var columnsItem = this.pickerDataType ? [{
        values: this.pickerData
      }] : this.pickerData;

      forEach._forEachInstanceProperty(columnsItem).call(columnsItem, function (columnValue, index) {
        _this.setColumnValues(index, JSON.parse(stringify._JSON$stringify(values$2(columnValue))));
      });
    },
    onChange: function onChange(columnIndex) {
      if (this.pickerDataType) {
        this.$emit('change', this, this.getColumnValue(0), this.getColumnIndex(0));
      } else {
        this.$emit('change', this, this.getValues(), columnIndex, this.getIndexes());
      }

      this.$emit('input', this.getValues());
    },
    cancel: function cancel() {
      this.$emit('cancel', false);
    },
    confirm: function confirm() {
      if (this.pickerDataType) {
        this.$emit('confirm', this, this.getColumnValue(0), this.getColumnIndex(0));
      } else {
        this.$emit('confirm', this, this.getValues(), this.getIndexes());
      }
    },
    getColumnValue: function getColumnValue(index) {
      var column = this.getColumn(index);
      return column && column.getValue();
    },
    setColumnValue: function setColumnValue(index, value) {
      var column = this.getColumn(index);

      if (column) {
        return column.setValue(value);
      }

      return false;
    },
    getColumn: function getColumn(index) {
      return this.children[index];
    },
    getColumnIndex: function getColumnIndex(columnIndex) {
      return (this.getColumn(columnIndex) || {}).currentIndex;
    },
    setColumnIndex: function setColumnIndex(columnIndex, optionIndex) {
      var column = this.getColumn(columnIndex);

      if (column) {
        return column.setOptionIndex(optionIndex);
      }

      return false;
    },
    getColumnValues: function getColumnValues(index) {
      return (this.children[index] || {}).options;
    },
    setColumnValues: function setColumnValues(index, options) {
      var column = this.children[index];

      if (column && stringify._JSON$stringify(column.options) !== stringify._JSON$stringify(options)) {
        column.options = options;
        column.setOptionIndex(0);
        this.onChange(index);
      }
    },
    getValues: function getValues() {
      var _context;

      return map._mapInstanceProperty(_context = this.children).call(_context, function (child) {
        return child.getValue();
      });
    },
    setValues: function setValues(values) {
      var _this2 = this;

      forEach._forEachInstanceProperty(values).call(values, function (value, index) {
        _this2.setColumnValue(index, value);
      });
    },
    getIndexes: function getIndexes() {
      var _context2;

      return map._mapInstanceProperty(_context2 = this.children).call(_context2, function (child) {
        return child.currentIndex;
      });
    },
    setIndexes: function setIndexes(indexes) {
      var _this3 = this;

      forEach._forEachInstanceProperty(indexes).call(indexes, function (optionIndex, columnIndex) {
        _this3.setColumnIndex(columnIndex, optionIndex);
      });
    }
  },
  mounted: function mounted() {
    var _this4 = this;

    this.$nextTick(function () {
      if (_this4.value && _this4.value.length) {
        _this4.setValues(_this4.value);
      }
    });
  }
});

var __vue_script__$1 = script$1;
/* template */

var __vue_render__$1 = function __vue_render__() {
  var _vm = this;

  var _h = _vm.$createElement;

  var _c = _vm._self._c || _h;

  return _c('div', {
    staticClass: "bx-picker"
  }, [_c('transition', {
    attrs: {
      "name": "bx-picker"
    }
  }, [_c('div', {
    staticClass: "bx-picker-body"
  }, [_vm.showToolbar ? _c('div', {
    staticClass: "bx-picker-header bx-hairline--bottom"
  }, [_c('p', {
    staticClass: "bx-picker-cancel",
    on: {
      "click": function click($event) {
        _vm.cancel();
      }
    }
  }, [_vm._v("\n          取消\n        ")]), _c('p', {
    staticClass: "bx-picker-title"
  }, [_vm._v("\n          " + _vm._s(_vm.title) + "\n        ")]), _c('p', {
    staticClass: "bx-picker-sure",
    on: {
      "click": function click($event) {
        _vm.confirm();
      }
    }
  }, [_vm._v("\n          确定\n        ")])]) : _vm._e(), _c('div', {
    staticClass: "bx-picker-content"
  }, [_c('div', {
    ref: "pickerWrapper",
    staticClass: "bx-picker-wrapper"
  }, _vm._l(_vm.pickerDataList, function (data, index) {
    return _c('picker-item', {
      key: index,
      attrs: {
        "values": _vm.pickerDataType ? data : values$2(data),
        "class-name": data.className,
        "default-index": data.defaultIndex,
        "value-key": _vm.valueKey
      },
      on: {
        "change": function change($event) {
          _vm.onChange(index);
        }
      }
    });
  })), _c('div', {
    staticClass: "bx-picker-center-highlight"
  })]), _c('div', {
    staticClass: "bx-picker-footer"
  })])])], 1);
};

var __vue_staticRenderFns__$1 = [];
/* style */

var __vue_inject_styles__$1 = undefined;
/* scoped */

var __vue_scope_id__$1 = undefined;
/* module identifier */

var __vue_module_identifier__$1 = undefined;
/* functional template */

var __vue_is_functional_template__$1 = false;
/* style inject */

/* style inject SSR */

var PickerView = normalizeComponent.__vue_normalize__({
  render: __vue_render__$1,
  staticRenderFns: __vue_staticRenderFns__$1
}, __vue_inject_styles__$1, __vue_script__$1, __vue_scope_id__$1, __vue_is_functional_template__$1, __vue_module_identifier__$1, undefined, undefined);

exports.PickerView = PickerView;
