var _makePropsConfigurabl;
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 { extend } from '../../vue';
import { NAME_TOOLTIP } from '../../constants/components';
import { EVENT_NAME_CLOSE, EVENT_NAME_DISABLE, EVENT_NAME_DISABLED, EVENT_NAME_ENABLE, EVENT_NAME_ENABLED, EVENT_NAME_HIDDEN, EVENT_NAME_HIDE, EVENT_NAME_OPEN, EVENT_NAME_SHOW, EVENT_NAME_SHOWN, MODEL_EVENT_NAME_PREFIX } from '../../constants/events';
import { PROP_TYPE_ARRAY_STRING, PROP_TYPE_BOOLEAN, PROP_TYPE_FUNCTION, PROP_TYPE_NUMBER_OBJECT_STRING, PROP_TYPE_NUMBER_STRING, PROP_TYPE_OBJECT, PROP_TYPE_STRING } from '../../constants/props';
import { HTMLElement, SVGElement } from '../../constants/safe-types';
import { useParentMixin } from '../../mixins/use-parent';
import { getScopeId } from '../../utils/get-scope-id';
import { isUndefinedOrNull } from '../../utils/inspect';
import { pick } from '../../utils/object';
import { makeProp, makePropsConfigurable } from '../../utils/props';
import { createNewChildComponent } from '../../utils/create-new-child-component';
import { normalizeSlotMixin } from '../../mixins/normalize-slot';
import { BVTooltip } from './helpers/bv-tooltip';

// --- Constants ---

var MODEL_PROP_NAME_ENABLED = 'disabled';
var MODEL_EVENT_NAME_ENABLED = MODEL_EVENT_NAME_PREFIX + MODEL_PROP_NAME_ENABLED;
var MODEL_PROP_NAME_SHOW = 'show';
var MODEL_EVENT_NAME_SHOW = MODEL_EVENT_NAME_PREFIX + MODEL_PROP_NAME_SHOW;

// --- Props ---

export var props = makePropsConfigurable((_makePropsConfigurabl = {
  // String: scrollParent, window, or viewport
  // Element: element reference
  // Object: Vue component
  boundary: makeProp([HTMLElement, PROP_TYPE_OBJECT, PROP_TYPE_STRING], 'scrollParent'),
  boundaryPadding: makeProp(PROP_TYPE_NUMBER_STRING, 50),
  // String: HTML ID of container, if null body is used (default)
  // HTMLElement: element reference reference
  // Object: Vue Component
  container: makeProp([HTMLElement, PROP_TYPE_OBJECT, PROP_TYPE_STRING]),
  customClass: makeProp(PROP_TYPE_STRING),
  delay: makeProp(PROP_TYPE_NUMBER_OBJECT_STRING, 50)
}, _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_makePropsConfigurabl, MODEL_PROP_NAME_ENABLED, makeProp(PROP_TYPE_BOOLEAN, false)), "fallbackPlacement", makeProp(PROP_TYPE_ARRAY_STRING, 'flip')), "id", makeProp(PROP_TYPE_STRING)), "noFade", makeProp(PROP_TYPE_BOOLEAN, false)), "noninteractive", makeProp(PROP_TYPE_BOOLEAN, false)), "offset", makeProp(PROP_TYPE_NUMBER_STRING, 0)), "placement", makeProp(PROP_TYPE_STRING, 'top')), MODEL_PROP_NAME_SHOW, makeProp(PROP_TYPE_BOOLEAN, false)), "target", makeProp([HTMLElement, SVGElement, PROP_TYPE_FUNCTION, PROP_TYPE_OBJECT, PROP_TYPE_STRING], undefined, true)), "title", makeProp(PROP_TYPE_STRING)), _defineProperty(_defineProperty(_makePropsConfigurabl, "triggers", makeProp(PROP_TYPE_ARRAY_STRING, 'hover focus')), "variant", makeProp(PROP_TYPE_STRING))), NAME_TOOLTIP);

// --- Main component ---

// @vue/component
export var BTooltip = /*#__PURE__*/extend({
  name: NAME_TOOLTIP,
  mixins: [normalizeSlotMixin, useParentMixin],
  inheritAttrs: false,
  props: props,
  data: function data() {
    return {
      localShow: this[MODEL_PROP_NAME_SHOW],
      localTitle: '',
      localContent: ''
    };
  },
  computed: {
    // Data that will be passed to the template and popper
    templateData: function templateData() {
      return _objectSpread({
        title: this.localTitle,
        content: this.localContent,
        interactive: !this.noninteractive
      }, pick(this.$props, ['boundary', 'boundaryPadding', 'container', 'customClass', 'delay', 'fallbackPlacement', 'id', 'noFade', 'offset', 'placement', 'target', 'target', 'triggers', 'variant', MODEL_PROP_NAME_ENABLED]));
    },
    // Used to watch for changes to the title and content props
    templateTitleContent: function templateTitleContent() {
      var title = this.title,
        content = this.content;
      return {
        title: title,
        content: content
      };
    }
  },
  watch: _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, MODEL_PROP_NAME_SHOW, function (newValue, oldValue) {
    if (newValue !== oldValue && newValue !== this.localShow && this.$_toolpop) {
      if (newValue) {
        this.$_toolpop.show();
      } else {
        // We use `forceHide()` to override any active triggers
        this.$_toolpop.forceHide();
      }
    }
  }), MODEL_PROP_NAME_ENABLED, function (newValue) {
    if (newValue) {
      this.doDisable();
    } else {
      this.doEnable();
    }
  }), "localShow", function localShow(newValue) {
    // TODO: May need to be done in a `$nextTick()`
    this.$emit(MODEL_EVENT_NAME_SHOW, newValue);
  }), "templateData", function templateData() {
    var _this = this;
    this.$nextTick(function () {
      if (_this.$_toolpop) {
        _this.$_toolpop.updateData(_this.templateData);
      }
    });
  }), "templateTitleContent", function templateTitleContent() {
    this.$nextTick(this.updateContent);
  }),
  created: function created() {
    // Create private non-reactive props
    this.$_toolpop = null;
  },
  updated: function updated() {
    // Update the `propData` object
    // Done in a `$nextTick()` to ensure slot(s) have updated
    this.$nextTick(this.updateContent);
  },
  beforeDestroy: function beforeDestroy() {
    // Shutdown our local event listeners
    this.$off(EVENT_NAME_OPEN, this.doOpen);
    this.$off(EVENT_NAME_CLOSE, this.doClose);
    this.$off(EVENT_NAME_DISABLE, this.doDisable);
    this.$off(EVENT_NAME_ENABLE, this.doEnable);
    // Destroy the tip instance
    if (this.$_toolpop) {
      this.$_toolpop.$destroy();
      this.$_toolpop = null;
    }
  },
  mounted: function mounted() {
    var _this2 = this;
    // Instantiate a new BVTooltip instance
    // Done in a `$nextTick()` to ensure DOM has completed rendering
    // so that target can be found
    this.$nextTick(function () {
      // Load the on demand child instance
      var Component = _this2.getComponent();
      // Ensure we have initial content
      _this2.updateContent();
      // Pass down the scoped style attribute if available
      var scopeId = getScopeId(_this2) || getScopeId(_this2.bvParent);
      // Create the instance
      var $toolpop = _this2.$_toolpop = createNewChildComponent(_this2, Component, {
        // Pass down the scoped style ID
        _scopeId: scopeId || undefined
      });
      // Set the initial data
      $toolpop.updateData(_this2.templateData);
      // Set listeners
      $toolpop.$on(EVENT_NAME_SHOW, _this2.onShow);
      $toolpop.$on(EVENT_NAME_SHOWN, _this2.onShown);
      $toolpop.$on(EVENT_NAME_HIDE, _this2.onHide);
      $toolpop.$on(EVENT_NAME_HIDDEN, _this2.onHidden);
      $toolpop.$on(EVENT_NAME_DISABLED, _this2.onDisabled);
      $toolpop.$on(EVENT_NAME_ENABLED, _this2.onEnabled);
      // Initially disabled?
      if (_this2[MODEL_PROP_NAME_ENABLED]) {
        // Initially disabled
        _this2.doDisable();
      }
      // Listen to open signals from others
      _this2.$on(EVENT_NAME_OPEN, _this2.doOpen);
      // Listen to close signals from others
      _this2.$on(EVENT_NAME_CLOSE, _this2.doClose);
      // Listen to disable signals from others
      _this2.$on(EVENT_NAME_DISABLE, _this2.doDisable);
      // Listen to enable signals from others
      _this2.$on(EVENT_NAME_ENABLE, _this2.doEnable);
      // Initially show tooltip?
      if (_this2.localShow) {
        $toolpop.show();
      }
    });
  },
  methods: {
    getComponent: function getComponent() {
      // Overridden by BPopover
      return BVTooltip;
    },
    updateContent: function updateContent() {
      // Overridden by BPopover
      // Tooltip: Default slot is `title`
      // Popover: Default slot is `content`, `title` slot is title
      // We pass a scoped slot function reference by default (Vue v2.6x)
      // And pass the title prop as a fallback
      this.setTitle(this.normalizeSlot() || this.title);
    },
    // Helper methods for `updateContent()`
    setTitle: function setTitle(value) {
      value = isUndefinedOrNull(value) ? '' : value;
      // We only update the value if it has changed
      if (this.localTitle !== value) {
        this.localTitle = value;
      }
    },
    setContent: function setContent(value) {
      value = isUndefinedOrNull(value) ? '' : value;
      // We only update the value if it has changed
      if (this.localContent !== value) {
        this.localContent = value;
      }
    },
    // --- Template event handlers ---
    onShow: function onShow(bvEvent) {
      // Placeholder
      this.$emit(EVENT_NAME_SHOW, bvEvent);
      if (bvEvent) {
        this.localShow = !bvEvent.defaultPrevented;
      }
    },
    onShown: function onShown(bvEvent) {
      // Tip is now showing
      this.localShow = true;
      this.$emit(EVENT_NAME_SHOWN, bvEvent);
    },
    onHide: function onHide(bvEvent) {
      this.$emit(EVENT_NAME_HIDE, bvEvent);
    },
    onHidden: function onHidden(bvEvent) {
      // Tip is no longer showing
      this.$emit(EVENT_NAME_HIDDEN, bvEvent);
      this.localShow = false;
    },
    onDisabled: function onDisabled(bvEvent) {
      // Prevent possible endless loop if user mistakenly
      // fires `disabled` instead of `disable`
      if (bvEvent && bvEvent.type === EVENT_NAME_DISABLED) {
        this.$emit(MODEL_EVENT_NAME_ENABLED, true);
        this.$emit(EVENT_NAME_DISABLED, bvEvent);
      }
    },
    onEnabled: function onEnabled(bvEvent) {
      // Prevent possible endless loop if user mistakenly
      // fires `enabled` instead of `enable`
      if (bvEvent && bvEvent.type === EVENT_NAME_ENABLED) {
        this.$emit(MODEL_EVENT_NAME_ENABLED, false);
        this.$emit(EVENT_NAME_ENABLED, bvEvent);
      }
    },
    // --- Local event listeners ---
    doOpen: function doOpen() {
      !this.localShow && this.$_toolpop && this.$_toolpop.show();
    },
    doClose: function doClose() {
      this.localShow && this.$_toolpop && this.$_toolpop.hide();
    },
    doDisable: function doDisable() {
      this.$_toolpop && this.$_toolpop.disable();
    },
    doEnable: function doEnable() {
      this.$_toolpop && this.$_toolpop.enable();
    }
  },
  render: function render(h) {
    // Always renders a comment node
    // TODO:
    //   Future: Possibly render a target slot (single root element)
    //   which we can apply the listeners to (pass `this.$el` to BVTooltip)
    return h();
  }
});