/*! 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/array-iteration.js';
import '../utils/sloppy-array-method.js';
import { _ as _forEachInstanceProperty } from '../utils/for-each.js';
import '../utils/index-of.js';
import '../utils/string-trim.js';
import { _ as _parseInt } from '../utils/parse-int.js';
import '../utils/filter.js';
import { _ as _extends } from '../utils/extends.js';
import BScroll from 'better-scroll';
import { P as Popup } from '../utils/index.js';
import '../utils/some.js';
import 'vue';
import '../utils/env.js';
import '../safe-area-patch/index.js';
import Icon from '../icon/index.js';

var _components;
var MAX_HEIGHT_RATE = 0.7,
    MIN_HEIGHT_RATE = 0.4;
var script = create({
  name: 'bottom-sheets',
  mixins: [Popup],
  props: {
    // 半屏浮层的固定高度
    height: {
      type: Number,
      default: null
    },
    // 是否使用better-scroll
    useBScroll: {
      type: Boolean,
      default: false
    },
    // 是否隐藏浮层顶部栏
    hideHeader: {
      type: Boolean,
      default: false
    },
    // 是否隐藏浮层底部栏
    hideFooter: {
      type: Boolean,
      default: false
    },
    // 顶部栏标题
    title: {
      type: String,
      default: ''
    }
  },
  computed: {
    transitionListeners: function transitionListeners() {
      var existEvents = {
        appear: this.enter,
        enter: this.enter,
        'after-enter': this.afterEnter
      };
      return _extends({}, this.listenersEmit(['appear', 'before-enter', 'enter', 'after-enter', 'enter-cancelled', 'leave', 'after-leave', 'leave-cancelled'], existEvents));
    }
  },
  methods: {
    // 关闭浮层
    closePopup: function closePopup() {
      this.$emit('input', false);
    },
    // 设置正文内容的位置
    setContentPosition: function setContentPosition() {
      var $header = this.$refs.header;
      var $footer = this.$refs.footer;
      this.headerHeight = 0;

      if ($header) {
        this.headerHeight = $header.clientHeight;
        this.wrapper.style.top = $header.clientHeight + "px";
      }

      if ($footer) {
        this.wrapper.style.bottom = $footer.clientHeight + "px";
      }
    },
    setHeight: function setHeight(el, height) {
      var $el = el;
      $el.style.height = height + "px";
    },
    // 设置浮层的高度最大70%，最小40%
    setLayerHeight: function setLayerHeight() {
      var $scroll = this.wrapper.children[0];

      var layerHeight = _parseInt($scroll.clientHeight + this.headerHeight, 10);

      var maxHeight = _parseInt(window.screen.availHeight * MAX_HEIGHT_RATE, 10);

      var minHeight = _parseInt(window.screen.availHeight * MIN_HEIGHT_RATE, 10);

      if (this.height) {
        var displayHeight = this.height;

        if (displayHeight >= maxHeight) {
          displayHeight = maxHeight;
        }

        if (displayHeight <= minHeight) {
          displayHeight = minHeight;
        }

        this.setHeight(this.layer, displayHeight);
      } else if (layerHeight >= maxHeight) {
        this.setHeight(this.layer, maxHeight);
      } else if (layerHeight <= minHeight) {
        this.setHeight(this.layer, minHeight);
      } else {
        this.setHeight(this.layer, layerHeight);
      }
    },
    initPopup: function initPopup() {
      this.layer = this.$refs.layer;
      this.wrapper = this.$refs.content;

      if (this.layer) {
        this.setContentPosition();
        this.setLayerHeight();
        this.$emit('domReady', this.$refs);
      }
    },
    // 动画enter回调
    enter: function enter() {
      this.initPopup();
    },
    // 动画after-enter回调
    afterEnter: function afterEnter() {
      // TODO: （#83）无视滚动区域是否超出父容器，一律初始化 BSCroll，用以支持浮层打开情况下内容动态变动的情形
      // 仅确定提供 BSCroll 实例，使用者仍需在内容变动后自行调用 scroll.refresh()
      // 因 BSCroll 只关心默认 slot 的内容，header 和 footer slot 中的内容变动暂无法支持
      // 后期可能需要更周到处理
      if (this.useBScroll) {
        // eslint-disable-next-line
        var scroll = new BScroll(this.wrapper);
        this.$emit('bscroll', scroll);
      }
    },
    // 给事件添加 $emit
    listenersEmit: function listenersEmit(eventNames, existEvents) {
      var _this = this;

      var listeners = {};

      _forEachInstanceProperty(eventNames).call(eventNames, function (name) {
        var eventFunc = existEvents[name];

        listeners[name] = function (el) {
          if (eventFunc) {
            eventFunc.call(_this, el);
          }

          _this.$emit(name, el);
        };
      });

      return listeners;
    }
  },
  components: (_components = {}, _components[Icon.name] = Icon, _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('transition', _vm._g({
    attrs: {
      "name": "bx-animate-slide"
    }
  }, _vm.transitionListeners), [_vm.value ? _c('div', {
    ref: "layer",
    staticClass: "bx-sheets-layer"
  }, [!_vm.hideHeader ? _c('div', {
    ref: "header",
    staticClass: "bx-sheets-header"
  }, [_vm._t("header", [_c('div', {
    staticClass: "bx-sheets-header-inner"
  }, [_c('div', {
    staticClass: "bx-sheets-title"
  }, [_vm._v("\n            " + _vm._s(_vm.title) + "\n          ")]), _c('div', {
    staticClass: "bx-sheets-close",
    on: {
      "click": _vm.closePopup
    }
  }, [_c('bxs-icon', {
    attrs: {
      "name": "close"
    }
  })], 1)])])], 2) : _vm._e(), _c('div', {
    ref: "content",
    staticClass: "bx-sheets-content",
    class: !_vm.useBScroll && 'bx-sheets-content--scroll'
  }, [_c('div', {
    ref: "scroll",
    staticClass: "bx-sheets-scroll"
  }, [_vm._t("default")], 2)]), !_vm.hideFooter ? _c('div', {
    ref: "footer",
    staticClass: "bx-sheets-footer"
  }, [_vm._t("footer")], 2) : _vm._e()]) : _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 bottomSheets = __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 bottomSheets;
