// This file is part of Indico.
// Copyright (C) 2002 - 2021 CERN
//
// Indico is free software; you can redistribute it and/or
// modify it under the terms of the MIT License; see the
// LICENSE file for more details.

/* global $T:false, alertPopup:false, handleAjaxError:false, ajaxDialog:false */

import React from 'react';
import ReactDOM from 'react-dom';

import 'indico/legacy/angular';

import './invitations';
import './reglists';

import './form/form';
import './form/section';
import './form/field';
import './form/sectiontoolbar';
import './form/table';

import './form/templates';

import RegistrationTagsEditableList from './components/RegistrationTagsEditableList';

(function(global) {
  $(document).ready(function() {
    setupRegistrationFormScheduleDialogs();
    setupRegistrationFormSummaryPage();
  });

  $(window).scroll(function() {
    IndicoUI.Effect.followScroll();
  });

  function setupRegistrationFormScheduleDialogs() {
    $('a.js-regform-schedule-dialog').on('click', function(e) {
      e.preventDefault();
      ajaxDialog({
        url: $(this).data('href'),
        title: $(this).data('title'),
        onClose(data) {
          if (data) {
            location.reload();
          }
        },
      });
    });
  }

  function setupRegistrationFormSummaryPage() {
    $('.js-check-conditions').on('click', function(e) {
      const conditions = $('#conditions-accepted');
      if (conditions.length && !conditions.prop('checked')) {
        const msg =
          'Please, confirm that you have read and accepted the Terms and Conditions before proceeding.';
        alertPopup($T.gettext(msg), $T.gettext('Terms and Conditions'));
        e.preventDefault();
      }
    });

    $('.js-highlight-payment').on('click', function() {
      $('#payment-summary').effect('highlight', 800);
    });
  }

  global.setupRegistrationFormListPage = function setupRegistrationFormListPage() {
    $('#payment-disabled-notice').on('indico:confirmed', '.js-enable-payments', function(evt) {
      evt.preventDefault();

      const $this = $(this);
      $.ajax({
        url: $this.data('href'),
        method: $this.data('method'),
        complete: IndicoUI.Dialogs.Util.progress(),
        error: handleAjaxError,
        success(data) {
          $('#payment-disabled-notice').remove();
          $('#event-side-menu').html(data.event_menu);
        },
      });
    });
  };

  document.addEventListener('DOMContentLoaded', () => {
    const rootElement = document.getElementById('registration-detail-registration-tags-assign');

    if (rootElement) {
      const assignedTags = JSON.parse(rootElement.dataset.assignedTags);
      const allTags = JSON.parse(rootElement.dataset.allTags);
      const {eventId, regformId, registrationId} = rootElement.dataset;

      ReactDOM.render(
        <RegistrationTagsEditableList
          eventId={parseInt(eventId, 10)}
          regformId={parseInt(regformId, 10)}
          registrationId={parseInt(registrationId, 10)}
          assignedTags={assignedTags}
          allTags={allTags}
        />,
        rootElement
      );
    }
  });
})(window);

@@ -2415,59 +2415,40 @@
 %0A%0A  
-document.addEventListener('DOMContentLoaded', () =%3E
+function setupRegistrationTags()
  %7B%0A 
@@ -3094,16 +3094,209 @@
 ;%0A    %7D%0A
+  %7D%0A%0A  document.addEventListener('DOMContentLoaded', () =%3E %7B%0A    setupRegistrationTags();%0A    $('#registration-details')%0A      .parent()%0A      .on('indico:htmlUpdated', setupRegistrationTags);%0A
   %7D);%0A%7D)

Fix registration tag manager after checkin etc