/*! bxs-vue-ui v3.3.0 */
import '../utils/entry-virtual.js';
import { C as create, _ as __vue_normalize__ } from '../utils/normalize-component.js';
import '../utils/object-create.js';
import '../utils/html.js';
import '../utils/es.number.constructor.js';
import '../utils/es.object.to-string.js';
import '../utils/array-iteration.js';
import '../utils/sloppy-array-method.js';
import { _ as _forEachInstanceProperty } from '../utils/for-each.js';
import { _ as _mapInstanceProperty } from '../utils/map.js';
import { _ as _indexOfInstanceProperty } from '../utils/index-of.js';
import '../utils/string-trim.js';
import { _ as _parseInt } from '../utils/parse-int.js';
import { _ as _filterInstanceProperty } from '../utils/filter.js';
import { _ as _Array$isArray } from '../utils/is-array.js';
import { _ as _sliceInstanceProperty } from '../utils/slice.js';
import '../utils/extends.js';
import 'better-scroll';
import '../utils/index.js';
import '../utils/some.js';
import 'vue';
import '../utils/env.js';
import '../safe-area-patch/index.js';
import '../icon/index.js';
import bottomSheets from '../bottom-sheets/index.js';

var _components;
var script = create({
  name: 'area',
  props: {
    showDistrict: {
      type: Boolean,
      default: true
    },
    keyName: {
      type: String,
      default: 'code'
    },
    valueName: {
      type: String,
      default: 'name'
    },
    areaData: {
      type: Object,
      default: function _default() {
        return {};
      }
    },
    areaSelect: {
      type: [String, Number],
      default: ''
    },
    title: {
      type: String,
      default: '选择地区'
    },
    value: {
      type: Boolean,
      default: false
    }
  },
  data: function data() {
    return {
      tabActive: 'provinceItem',
      provinceCode: '',
      provinceName: '',
      cityCode: '',
      cityName: '',
      countyCode: '',
      countyName: '',
      provinceList: [],
      cityList: [],
      countyList: [],
      $content: null,
      finish: false
    };
  },
  computed: {
    isSelectionDone: function isSelectionDone() {
      return this.provinceCode && this.cityCode && (this.countyCode || !this.showDistrict || this.finish);
    },
    isShow: {
      get: function get() {
        return this.value;
      },
      set: function set(val) {
        this.$emit('input', val);
      }
    }
  },
  watch: {
    isShow: function isShow(val) {
      if (val) {
        this.init();
      }
    }
  },
  methods: {
    close: function close() {
      this.isShow = false;
    },
    init: function init() {
      this.provinceList = this.areaData.provinceList || [];
      this.setValues();
    },
    setValues: function setValues() {
      var _context, _context2;

      var proCode = _sliceInstanceProperty(_context = this.areaSelect.toString()).call(_context, 0, 2);

      var cityCode = _sliceInstanceProperty(_context2 = this.areaSelect.toString()).call(_context2, 0, 4);

      var vm = this;

      if (proCode && _Array$isArray(this.areaData.provinceList)) {
        var _context3, _context4, _context5, _context6;

        var _this$areaData$provin = _filterInstanceProperty(_context3 = this.areaData.provinceList).call(_context3, function (item) {
          return _parseInt(item[vm.keyName], 10) === _parseInt(proCode + "0000", 10);
        }),
            proFilter = _this$areaData$provin[0];

        var _this$areaData$cityLi = _filterInstanceProperty(_context4 = this.areaData.cityList).call(_context4, function (item) {
          return _parseInt(item[vm.keyName], 10) === _parseInt(cityCode + "00", 10);
        }),
            cityFilter = _this$areaData$cityLi[0];

        var _this$areaData$county = _filterInstanceProperty(_context5 = this.areaData.countyList).call(_context5, function (item) {
          return _parseInt(item[vm.keyName], 10) === _parseInt(vm.areaSelect, 10);
        }),
            countyFilter = _this$areaData$county[0];

        if (!proFilter || !cityFilter) {
          this.reset();
          return;
        }

        vm.selectProvince(proFilter[vm.keyName], proFilter[vm.valueName]);

        if (_sliceInstanceProperty(_context6 = this.areaSelect.toString()).call(_context6, 2, 6) !== '0000' && cityCode) {
          var _context7;

          vm.selectCity(cityFilter[vm.keyName], cityFilter[vm.valueName]);
          vm.switchTab('cityItem');

          if (_sliceInstanceProperty(_context7 = this.areaSelect.toString()).call(_context7, 4, 6) !== '00' && vm.showDistrict) {
            if (countyFilter) {
              vm.selectCounty(countyFilter[vm.keyName], countyFilter[vm.valueName]);
              vm.switchTab('countyItem');
            } else {
              this.reset();
            }
          }
        }
      } else {
        this.reset();
      }
    },
    reset: function reset() {
      this.tabActive = 'provinceItem';
      this.provinceCode = '';
      this.provinceName = '';
      this.cityCode = '';
      this.cityName = '';
      this.countyCode = '';
      this.countyName = '';
    },
    switchTab: function switchTab(activeTab) {
      var _this = this;

      this.tabActive = activeTab;
      var indexItem = 0;

      if (activeTab === 'provinceItem') {
        if (this.provinceCode) {
          var _context8, _context9;

          indexItem = _indexOfInstanceProperty(_context8 = _mapInstanceProperty(_context9 = this.provinceList).call(_context9, function (item) {
            return item[_this.keyName];
          })).call(_context8, this.provinceCode);
        }
      } else if (activeTab === 'cityItem') {
        if (this.cityCode) {
          var _context10, _context11;

          indexItem = _indexOfInstanceProperty(_context10 = _mapInstanceProperty(_context11 = this.cityList).call(_context11, function (item) {
            return item[_this.keyName];
          })).call(_context10, this.cityCode);
        }
      } else if (activeTab === 'countyItem') {
        if (this.countyCode) {
          var _context12, _context13;

          indexItem = _indexOfInstanceProperty(_context12 = _mapInstanceProperty(_context13 = this.countyList).call(_context13, function (item) {
            return item[_this.keyName];
          })).call(_context12, this.countyCode);
        }
      }

      this.$nextTick(function () {
        if (indexItem) {
          if (_this.$refs.areaItem && _this.$refs.areaItem.length) {
            var areaItem = _this.$refs.areaItem && _this.$refs.areaItem[0].offsetHeight;

            _this.resetTop((indexItem - 2) * areaItem);
          }
        } else {
          _this.resetTop();
        }
      });
    },
    domReady: function domReady(dom) {
      this.$content = dom.content;
    },
    resetTop: function resetTop(y) {
      var _this2 = this;

      if (y === void 0) {
        y = 0;
      }

      this.$nextTick(function () {
        if (_this2.$content) {
          _this2.$content.scrollTop = y;
        }
      });
    },
    selectProvince: function selectProvince(provinceCode, provinceName) {
      var _context14,
          _this3 = this;

      if (this.provinceCode !== provinceCode) {
        this.provinceCode = provinceCode;
        this.provinceName = provinceName;
        this.cityCode = '';
        this.cityName = '';
        this.countyCode = '';
        this.countyName = '';
      }

      this.tabActive = 'cityItem';
      this.cityList = [];

      _forEachInstanceProperty(_context14 = this.areaData.cityList).call(_context14, function (city) {
        if (city.parentId === provinceCode) {
          _this3.cityList.push(city);
        }
      });

      this.resetTop();
    },
    selectCity: function selectCity(cityCode, cityName) {
      var _this4 = this;

      if (this.cityCode !== cityCode) {
        this.cityCode = cityCode;
        this.cityName = cityName;
        this.countyCode = '';
        this.countyName = '';
      }

      if (this.showDistrict) {
        var _context15;

        this.countyList = [];

        _forEachInstanceProperty(_context15 = this.areaData.countyList).call(_context15, function (county) {
          if (county.parentId === cityCode) {
            _this4.countyList.push(county);
          }
        });

        if (this.countyList.length) {
          this.tabActive = 'countyItem';
          this.resetTop();
          this.finish = false;
        } else {
          this.finish = true;
        }
      }
    },
    selectCounty: function selectCounty(countyCode, countyName) {
      this.countyCode = countyCode;
      this.countyName = countyName;
    },
    confirm: function confirm() {
      if (!this.isSelectionDone) {
        return;
      }

      this.$emit('confirm', {
        provinceCode: this.provinceCode,
        provinceName: this.provinceName,
        cityCode: this.cityCode,
        cityName: this.cityName,
        countyCode: this.countyCode,
        countyName: this.countyName
      });
      this.isShow = false;
    }
  },
  components: (_components = {}, _components[bottomSheets.name] = bottomSheets, _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: {
      "title": "选择地区",
      "height": 375,
      "prevent-scroll-through": true,
      "use-b-scroll": false
    },
    on: {
      "close": _vm.close,
      "domReady": _vm.domReady
    },
    model: {
      value: _vm.isShow,
      callback: function callback($$v) {
        _vm.isShow = $$v;
      },
      expression: "isShow"
    }
  }, [_c('div', {
    staticClass: "bx-area-header",
    attrs: {
      "slot": "header"
    },
    slot: "header"
  }, [_c('div', {
    staticClass: "bx-area-title"
  }, [_c('div', {
    staticClass: "bx-area-cancel",
    on: {
      "click": _vm.close
    }
  }, [_vm._v("\n        取消\n      ")]), _c('div', [_vm._v(_vm._s(_vm.title))]), _c('div', {
    staticClass: "bx-area-confirm",
    class: {
      'bx-area-confirm--primary': _vm.isSelectionDone
    },
    on: {
      "click": _vm.confirm
    }
  }, [_vm._v("\n        确定\n      ")])]), _c('div', {
    staticClass: "bx-area-tabs bx-hairline--bottom"
  }, [_c('div', {
    staticClass: "bx-area-tab",
    class: {
      'bx-area-active': _vm.tabActive === 'provinceItem'
    },
    on: {
      "click": function click($event) {
        _vm.switchTab('provinceItem');
      }
    }
  }, [_vm._v("\n        " + _vm._s(_vm.provinceName || '请选择') + "\n      ")]), _vm.provinceName ? _c('div', {
    staticClass: "bx-area-tab",
    class: {
      'bx-area-active': _vm.tabActive === 'cityItem'
    },
    on: {
      "click": function click($event) {
        _vm.switchTab('cityItem');
      }
    }
  }, [_vm._v("\n        " + _vm._s(_vm.cityName || '请选择') + "\n      ")]) : _vm._e(), _vm.cityName && _vm.showDistrict && !_vm.finish ? _c('div', {
    staticClass: "bx-area-tab",
    class: {
      'bx-area-active': _vm.tabActive === 'countyItem'
    },
    on: {
      "click": function click($event) {
        _vm.switchTab('countyItem');
      }
    }
  }, [_vm._v("\n        " + _vm._s(_vm.countyName || '请选择') + "\n      ")]) : _vm._e()])]), _c('div', {
    ref: "areaContainer",
    staticClass: "bx-area-container"
  }, [_vm.tabActive === 'provinceItem' ? _c('ul', {
    staticClass: "bx-area-items"
  }, _vm._l(_vm.provinceList, function (item) {
    return _c('li', {
      key: item[_vm.keyName],
      ref: "areaItem",
      refInFor: true,
      staticClass: "bx-area-item",
      class: {
        'bx-area-active': item[_vm.keyName] === _vm.provinceCode
      },
      on: {
        "click": function click($event) {
          _vm.selectProvince(item[_vm.keyName], item[_vm.valueName]);
        }
      }
    }, [_vm._v("\n        " + _vm._s(item[_vm.valueName]) + "\n      ")]);
  })) : _vm._e(), _vm.tabActive === 'cityItem' ? _c('ul', {
    staticClass: "bx-area-items"
  }, _vm._l(_vm.cityList, function (item) {
    return _c('li', {
      key: item[_vm.keyName],
      ref: "areaItem",
      refInFor: true,
      staticClass: "bx-area-item",
      class: {
        'bx-area-active': item[_vm.keyName] === _vm.cityCode
      },
      on: {
        "click": function click($event) {
          _vm.selectCity(item[_vm.keyName], item[_vm.valueName]);
        }
      }
    }, [_vm._v("\n        " + _vm._s(item[_vm.valueName]) + "\n      ")]);
  })) : _vm._e(), _vm.tabActive === 'countyItem' && _vm.showDistrict && !_vm.finish ? _c('ul', {
    staticClass: "bx-area-items"
  }, _vm._l(_vm.countyList, function (item) {
    return _c('li', {
      key: item[_vm.keyName],
      ref: "areaItem",
      refInFor: true,
      staticClass: "bx-area-item",
      class: {
        'bx-area-active': item[_vm.keyName] === _vm.countyCode
      },
      on: {
        "click": function click($event) {
          _vm.selectCounty(item[_vm.keyName], item[_vm.valueName]);
        }
      }
    }, [_vm._v("\n        " + _vm._s(item[_vm.valueName]) + "\n      ")]);
  })) : _vm._e()])]);
};

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 index = __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);

export default index;
