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_IMG_LAZY } from '../../constants/components';
import { HAS_INTERACTION_OBSERVER_SUPPORT } from '../../constants/env';
import { MODEL_EVENT_NAME_PREFIX } from '../../constants/events';
import { PROP_TYPE_BOOLEAN, PROP_TYPE_NUMBER_STRING, PROP_TYPE_STRING } from '../../constants/props';
import { concat } from '../../utils/array';
import { requestAF } from '../../utils/dom';
import { identity } from '../../utils/identity';
import { toInteger } from '../../utils/number';
import { omit } from '../../utils/object';
import { makeProp, makePropsConfigurable, pluckProps } from '../../utils/props';
import { VBVisible } from '../../directives/visible/visible';
import { BImg, props as BImgProps } from './img';

// --- Constants ---

var MODEL_PROP_NAME_SHOW = 'show';
var MODEL_EVENT_NAME_SHOW = MODEL_EVENT_NAME_PREFIX + MODEL_PROP_NAME_SHOW;

// --- Props ---

var imgProps = omit(BImgProps, ['blank']);
export var props = makePropsConfigurable(_objectSpread(_objectSpread({}, imgProps), {}, _defineProperty({
  blankHeight: makeProp(PROP_TYPE_NUMBER_STRING),
  // If `null`, a blank image is generated
  blankSrc: makeProp(PROP_TYPE_STRING, null),
  blankWidth: makeProp(PROP_TYPE_NUMBER_STRING),
  // Distance away from viewport (in pixels)
  // before being considered "visible"
  offset: makeProp(PROP_TYPE_NUMBER_STRING, 360)
}, MODEL_PROP_NAME_SHOW, makeProp(PROP_TYPE_BOOLEAN, false))), NAME_IMG_LAZY);

// --- Main component ---

// @vue/component
export var BImgLazy = /*#__PURE__*/extend({
  name: NAME_IMG_LAZY,
  directives: {
    'b-visible': VBVisible
  },
  props: props,
  data: function data() {
    return {
      isShown: this[MODEL_PROP_NAME_SHOW]
    };
  },
  computed: {
    computedSrc: function computedSrc() {
      var blankSrc = this.blankSrc;
      return !blankSrc || this.isShown ? this.src : blankSrc;
    },
    computedBlank: function computedBlank() {
      return !(this.isShown || this.blankSrc);
    },
    computedWidth: function computedWidth() {
      var width = this.width;
      return this.isShown ? width : this.blankWidth || width;
    },
    computedHeight: function computedHeight() {
      var height = this.height;
      return this.isShown ? height : this.blankHeight || height;
    },
    computedSrcset: function computedSrcset() {
      var srcset = concat(this.srcset).filter(identity).join(',');
      return srcset && (!this.blankSrc || this.isShown) ? srcset : null;
    },
    computedSizes: function computedSizes() {
      var sizes = concat(this.sizes).filter(identity).join(',');
      return sizes && (!this.blankSrc || this.isShown) ? sizes : null;
    }
  },
  watch: _defineProperty(_defineProperty({}, MODEL_PROP_NAME_SHOW, function (newValue, oldValue) {
    if (newValue !== oldValue) {
      // If `IntersectionObserver` support is not available, image is always shown
      var visible = HAS_INTERACTION_OBSERVER_SUPPORT ? newValue : true;
      this.isShown = visible;

      // Ensure the show prop is synced (when no `IntersectionObserver`)
      if (newValue !== visible) {
        this.$nextTick(this.updateShowProp);
      }
    }
  }), "isShown", function isShown(newValue, oldValue) {
    // Update synched show prop
    if (newValue !== oldValue) {
      this.updateShowProp();
    }
  }),
  mounted: function mounted() {
    var _this = this;
    // If `IntersectionObserver` is not available, image is always shown
    this.$nextTick(function () {
      _this.isShown = HAS_INTERACTION_OBSERVER_SUPPORT ? _this[MODEL_PROP_NAME_SHOW] : true;
    });
  },
  methods: {
    updateShowProp: function updateShowProp() {
      this.$emit(MODEL_EVENT_NAME_SHOW, this.isShown);
    },
    doShow: function doShow(visible) {
      var _this2 = this;
      // If IntersectionObserver is not supported, the callback
      // will be called with `null` rather than `true` or `false`
      if ((visible || visible === null) && !this.isShown) {
        // In a `requestAF()` to render the `blank` placeholder properly
        // for fast loading images in some browsers (i.e. Firefox)
        requestAF(function () {
          _this2.isShown = true;
        });
      }
    }
  },
  render: function render(h) {
    var directives = [];
    if (!this.isShown) {
      // We only add the visible directive if we are not shown
      directives.push({
        // Visible directive will silently do nothing if
        // `IntersectionObserver` is not supported
        name: 'b-visible',
        // Value expects a callback (passed one arg of `visible` = `true` or `false`)
        value: this.doShow,
        modifiers: _defineProperty(_defineProperty({}, "".concat(toInteger(this.offset, 0)), true), "once", true)
      });
    }
    return h(BImg, {
      directives: directives,
      props: _objectSpread(_objectSpread({}, pluckProps(imgProps, this.$props)), {}, {
        // Computed value props
        src: this.computedSrc,
        blank: this.computedBlank,
        width: this.computedWidth,
        height: this.computedHeight,
        srcset: this.computedSrcset,
        sizes: this.computedSizes
      })
    });
  }
});