;(function tb_widget_iife(w, d) {
  w.TB = w.TB || {};
  w.TB.Widgets = w.TB.Widgets || {
    loadedScripts: [],
    init: function (script, options) {
      var container = d.createElement('div');
      var id = w.TB.Widgets.loadedScripts.length;
      container.setAttribute('data-id', id);
      container.setAttribute('data-lang', options.language)
      container.id = 'tb-widget-container' + id;
      script.parentNode.insertBefore(container, script);

      if (id === 1) {
        var client = d.createElement('script');
        client.type = 'text/javascript';
        client.id = "TB-js-client";
        client.src = options.baseUrl + '/api/reservations/widgets/booking/assets/client.js';
        d.head.appendChild(client);

        var styles = d.createElement('link');
        styles.rel = 'stylesheet';
        styles.type = 'text/css';
        styles.href = options.baseUrl + '/api/reservations/widgets/booking/assets/styles.css';
        d.head.appendChild(styles);
      }

      // load initial html into container
      var fullUrl = options.baseUrl +
        '/api/reservations/widgets/booking/' + options.baseXRefId +
        '/rendering?format=' + options.format +
        '&language=' + options.language +
        '&id=' + id +
        '&branded=' + options.branded;

      if (options.preview) {
        fullUrl += '&preview=' + options.preview;
      }

      if (options.selectedVenues) {
        fullUrl += `&venues=${encodeURIComponent(options.selectedVenues.toString())}`;
      }

      fetch(fullUrl)
        .then(function (response) {
          return response.text();
        })
        .then(function (html) {
          container.innerHTML = html;

          // check if client is ready
          if (w.TB.init) { 
            w.TB.init(container, options);
          } else { 
            d.getElementById('TB-js-client').addEventListener('load', function() { 
              w.TB.init(container, options);
            });
          }
        });
    },
  }

  var scripts = d.getElementsByTagName('script')

  for (var i = 0; i < scripts.length; i++) {
    var script = scripts[i];
    var bookingOptions = { 
      format: 'row', 
      language: 'en', 
      baseXRefId: '55830',
      branded: false,
      baseUrl: 'https://order.tbdine.com',
      multilocation: false
    };
    
    

    

    ;/\/(reservations\/)?widgets\/booking\/55830\/injector/.test(script.src) &&
      w.TB.Widgets.loadedScripts.indexOf(script) < 0 &&
      (w.TB.Widgets.loadedScripts.push(script), w.TB.Widgets.init(script, bookingOptions));
  }
})(window, document);
