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

Object.defineProperty(exports, '__esModule', { value: true });

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

require('../utils/entry-virtual.js');
var normalizeComponent = require('../utils/normalize-component.js');
require('../utils/object-create.js');
require('../utils/html.js');
require('../utils/es.number.constructor.js');
require('../utils/array-iteration.js');
require('../utils/sloppy-array-method.js');
require('../utils/for-each.js');
require('../utils/map.js');
require('../utils/index-of.js');
require('../utils/string-trim.js');
require('../utils/parse-int.js');
require('../utils/filter.js');
var _extends = require('../utils/extends.js');
require('better-scroll');
require('../utils/index.js');
require('../utils/some.js');
var Vue = _interopDefault(require('vue'));
require('../utils/env.js');
require('../safe-area-patch/index.js');
require('../icon/index.js');
var bottomSheets = require('../bottom-sheets/index.js');
require('../utils/splice.js');
var picker = require('../utils/picker.js');
require('../utils/stringify.js');

var _components;
var script = normalizeComponent.create({
  name: 'pop-picker',
  props: {},
  data: function data() {
    return {
      isShow: false,
      title: '',
      showToolbar: true,
      pickerData: [],
      defaultValue: '',
      value: '',
      findValue: '',
      index: '',
      indexs: '',
      pickerObj: {},
      valueKey: 'text',
      isChange: false
    };
  },
  computed: {},
  methods: {
    cancel: function cancel() {
      this.isShow = false;
      this.$emit('cancel', false);
    },
    confirm: function confirm(pickerObj, value, index, indexs) {
      this.isShow = false;
      this.pickerObj = pickerObj;
      this.findValue = value;
      this.index = index;
      this.indexs = indexs;
      this.$emit('confirm', this.pickerObj, this.findValue, this.index, this.indexs);
    },
    sure: function sure() {
      if (!this.isChange) {
        this.$refs.bxsPicker.confirm();
        return;
      }

      this.isShow = false;
      this.$emit('confirm', this.pickerObj, this.findValue, this.index, this.indexs);
    },
    change: function change(pickerObj, value, index, indexs) {
      this.isChange = true;
      this.pickerObj = pickerObj;
      this.findValue = value;
      this.index = index;
      this.indexs = indexs;
      this.$emit('change', pickerObj, value, index, indexs);
    }
  },
  watch: {
    value: function value(n) {
      this.$emit('input', n);
    }
  },
  components: (_components = {}, _components[picker.PickerView.name] = picker.PickerView, _components[bottomSheets.default.name] = bottomSheets.default, _components)
});

/* 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('bxs-bottom-sheets', {
    attrs: {
      "prevent-scroll-through": true
    },
    on: {
      "after-leave": function afterLeave($event) {
        _vm.$emit('after-leave');
      }
    },
    model: {
      value: _vm.isShow,
      callback: function callback($$v) {
        _vm.isShow = $$v;
      },
      expression: "isShow"
    }
  }, [_c('div', {
    staticClass: "bx-picker-sheets-header",
    attrs: {
      "slot": "header"
    },
    slot: "header"
  }, [_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.sure();
      }
    }
  }, [_vm._v("\n        确定\n      ")])]) : _vm._e()]), _c('bxs-picker', {
    ref: "bxsPicker",
    attrs: {
      "picker-data": _vm.pickerData,
      "value": _vm.defaultValue,
      "value-key": _vm.valueKey
    },
    on: {
      "change": _vm.change,
      "confirm": _vm.confirm
    }
  })], 1);
};

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 PopPickerView = 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 PickerConstructor = Vue.extend(PopPickerView);
var instance;

var getAnInstance = function getAnInstance() {
  return new PickerConstructor({
    el: document.createElement('div')
  });
};

function Picker(options) {
  instance = getAnInstance();

  _extends._extends(instance, _extends._extends({}, options));

  instance.$on('confirm', function (picker, value, index, indexs) {
    if (typeof options.callback === 'function') {
      options.callback({
        type: 'confirm',
        picker: picker,
        value: value,
        index: index,
        indexs: indexs
      });
    }

    instance.$on('after-leave', function () {
      return instance.$destroy();
    });
  });
  instance.$on('change', function (picker, value, index) {
    if (typeof options.callback === 'function') {
      options.callback({
        type: 'change',
        picker: picker,
        value: value,
        index: index
      });
    }
  });
  instance.$on('input', function (value) {
    instance.value = value;
  });

  if (typeof options === 'string') {
    instance.title = options;
  }

  document.body.appendChild(instance.$el);
  instance.isShow = false;
  Vue.nextTick(function () {
    instance.isShow = true;
    instance.showToolbar = true;

    if (options.pickerData) {
      instance.index = options.index ? options.index : 0;
      instance.defaultValue = options.defaultValue ? options.defaultValue : [options.pickerData[instance.index]];
    }
  });
}

Picker.install = function (vueGlobal) {
  var vueGlobalObj = vueGlobal;
  vueGlobalObj.use(picker.PickerView);
  vueGlobalObj.use(PopPickerView);
  vueGlobalObj.prototype.$picker = Picker;
};

exports.default = Picker;
