function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import { NAME_ALERT } from '../../constants/components';
import { EVENT_NAME_DISMISSED, EVENT_NAME_DISMISS_COUNT_DOWN } from '../../constants/events';
import { PROP_TYPE_BOOLEAN, PROP_TYPE_BOOLEAN_NUMBER_STRING, PROP_TYPE_STRING } from '../../constants/props';
import { SLOT_NAME_DISMISS } from '../../constants/slots';
import { normalizeSlotMixin } from '../../mixins/normalize-slot';
import { requestAF } from '../../utils/dom';
import { isBoolean, isNumeric } from '../../utils/inspect';
import { makeModelMixin } from '../../utils/model';
import { toInteger } from '../../utils/number';
import { sortKeys } from '../../utils/object';
import { makeProp, makePropsConfigurable } from '../../utils/props';
import { COMPONENT_UID_KEY, extend } from '../../vue';
import { BButtonClose } from '../button/button-close';
import { BVTransition } from '../transition/bv-transition';

// --- Constants ---

var _makeModelMixin = makeModelMixin('show', {
    type: PROP_TYPE_BOOLEAN_NUMBER_STRING,
    defaultValue: false
  }),
  modelMixin = _makeModelMixin.mixin,
  modelProps = _makeModelMixin.props,
  MODEL_PROP_NAME = _makeModelMixin.prop,
  MODEL_EVENT_NAME = _makeModelMixin.event;

// --- Helper methods ---

// Convert `show` value to a number
var parseCountDown = function parseCountDown(show) {
  if (show === '' || isBoolean(show)) {
    return 0;
  }
  show = toInteger(show, 0);
  return show > 0 ? show : 0;
};

// Convert `show` value to a boolean
var parseShow = function parseShow(show) {
  if (show === '' || show === true) {
    return true;
  }
  if (toInteger(show, 0) < 1) {
    // Boolean will always return false for the above comparison
    return false;
  }
  return !!show;
};

// --- Props ---

export var props = makePropsConfigurable(sortKeys(_objectSpread(_objectSpread({}, modelProps), {}, {
  dismissLabel: makeProp(PROP_TYPE_STRING, 'Close'),
  dismissible: makeProp(PROP_TYPE_BOOLEAN, false),
  fade: makeProp(PROP_TYPE_BOOLEAN, false),
  variant: makeProp(PROP_TYPE_STRING, 'info')
})), NAME_ALERT);

// --- Main component ---

// @vue/component
export var BAlert = /*#__PURE__*/extend({
  name: NAME_ALERT,
  mixins: [modelMixin, normalizeSlotMixin],
  props: props,
  data: function data() {
    return {
      countDown: 0,
      // If initially shown, we need to set these for SSR
      localShow: parseShow(this[MODEL_PROP_NAME])
    };
  },
  watch: _defineProperty(_defineProperty(_defineProperty({}, MODEL_PROP_NAME, function (newValue) {
    this.countDown = parseCountDown(newValue);
    this.localShow = parseShow(newValue);
  }), "countDown", function countDown(newValue) {
    var _this = this;
    this.clearCountDownInterval();
    var show = this[MODEL_PROP_NAME];
    // Ignore if `show` transitions to a boolean value
    if (isNumeric(show)) {
      this.$emit(EVENT_NAME_DISMISS_COUNT_DOWN, newValue);
      // Update the v-model if needed
      if (show !== newValue) {
        this.$emit(MODEL_EVENT_NAME, newValue);
      }
      if (newValue > 0) {
        this.localShow = true;
        this.$_countDownTimeout = setTimeout(function () {
          _this.countDown--;
        }, 1000);
      } else {
        // Slightly delay the hide to allow any UI updates
        this.$nextTick(function () {
          requestAF(function () {
            _this.localShow = false;
          });
        });
      }
    }
  }), "localShow", function localShow(newValue) {
    var show = this[MODEL_PROP_NAME];
    // Only emit dismissed events for dismissible or auto-dismissing alerts
    if (!newValue && (this.dismissible || isNumeric(show))) {
      this.$emit(EVENT_NAME_DISMISSED);
    }
    // Only emit booleans if we weren't passed a number via v-model
    if (!isNumeric(show) && show !== newValue) {
      this.$emit(MODEL_EVENT_NAME, newValue);
    }
  }),
  created: function created() {
    // Create private non-reactive props
    this.$_filterTimer = null;
    var show = this[MODEL_PROP_NAME];
    this.countDown = parseCountDown(show);
    this.localShow = parseShow(show);
  },
  beforeDestroy: function beforeDestroy() {
    this.clearCountDownInterval();
  },
  methods: {
    dismiss: function dismiss() {
      this.clearCountDownInterval();
      this.countDown = 0;
      this.localShow = false;
    },
    clearCountDownInterval: function clearCountDownInterval() {
      clearTimeout(this.$_countDownTimeout);
      this.$_countDownTimeout = null;
    }
  },
  render: function render(h) {
    var $alert = h();
    if (this.localShow) {
      var dismissible = this.dismissible,
        variant = this.variant;
      var $dismissButton = h();
      if (dismissible) {
        // Add dismiss button
        $dismissButton = h(BButtonClose, {
          attrs: {
            'aria-label': this.dismissLabel
          },
          on: {
            click: this.dismiss
          }
        }, [this.normalizeSlot(SLOT_NAME_DISMISS)]);
      }
      $alert = h('div', {
        staticClass: 'alert',
        class: _defineProperty({
          'alert-dismissible': dismissible
        }, "alert-".concat(variant), variant),
        attrs: {
          role: 'alert',
          'aria-live': 'polite',
          'aria-atomic': true
        },
        key: this[COMPONENT_UID_KEY]
      }, [$dismissButton, this.normalizeSlot()]);
    }
    return h(BVTransition, {
      props: {
        noFade: !this.fade
      }
    }, [$alert]);
  }
});