/* global bootstrap, jQuery */
(function () {
  'use strict';

  if (typeof window === 'undefined' || typeof document === 'undefined') {
    return;
  }

  var $ = window.jQuery;
  var bs = window.bootstrap;

  // Keep compatibility with checkout scripts that call `$(modal).modal(...)`.
  if ($ && $.fn && typeof $.fn.modal !== 'function' && bs && typeof bs.Modal === 'function') {
    $.fn.modal = function (action) {
      return this.each(function () {
        var instance = bs.Modal.getOrCreateInstance(this);

        if (action === 'hide') {
          instance.hide();
          return;
        }

        instance.show();
      });
    };
  }
})();
