( function() {
  'use strict';

  function updateClassicContentVisibility() {
    var metaRoot = document.querySelector( '.pdt-content-visibility-metabox' );

    if ( metaRoot ) {
      var modeSelect = metaRoot.querySelector( '#pdt_content_visibility_mode' );
      var behaviorSelect = metaRoot.querySelector( '#pdt_content_visibility_denied_behavior' );
      var roleField = metaRoot.querySelector( '.pdt-content-visibility-role-field' );
      var redirectField = metaRoot.querySelector( '.pdt-content-visibility-redirect-field' );
      var messageField = metaRoot.querySelector( '.pdt-content-visibility-message-field' );
      var modeHelp = metaRoot.querySelector( '.pdt-content-visibility-mode-help' );
      var roleHelp = metaRoot.querySelector( '.pdt-content-visibility-role-help' );

      if ( modeSelect && behaviorSelect ) {
        var showRoles = modeSelect.value === 'show_roles' || modeSelect.value === 'hide_roles';
        var showRedirect = behaviorSelect.value === 'custom_redirect';
        var showMessage = behaviorSelect.value === 'message';

        if ( roleField ) {
          roleField.style.display = showRoles ? '' : 'none';
        }

        if ( modeHelp ) {
          modeHelp.textContent = modeSelect.value === 'show_roles' ? modeHelp.getAttribute( 'data-legacy-text' ) : modeHelp.getAttribute( 'data-default-text' );
        }

        if ( roleHelp ) {
          roleHelp.textContent = modeSelect.value === 'show_roles' ? roleHelp.getAttribute( 'data-legacy-text' ) : roleHelp.getAttribute( 'data-default-text' );
        }

        if ( redirectField ) {
          redirectField.style.display = showRedirect ? '' : 'none';
        }

        if ( messageField ) {
          messageField.style.display = showMessage ? '' : 'none';
        }
      }
    }

    var settingsBehaviorSelect = document.getElementById( 'content_visibility_default_denied_behavior' );
    var defaultRedirectRow = document.querySelector( '.pdt-content-visibility-default-redirect-row' );
    var defaultMessageRow = document.querySelector( '.pdt-content-visibility-default-message-row' );

    if ( settingsBehaviorSelect ) {
      var showDefaultRedirect = settingsBehaviorSelect.value === 'custom_redirect';
      var showDefaultMessage = settingsBehaviorSelect.value === 'message';

      if ( defaultRedirectRow ) {
        defaultRedirectRow.style.display = showDefaultRedirect ? '' : 'none';
      }

      if ( defaultMessageRow ) {
        defaultMessageRow.style.display = showDefaultMessage ? '' : 'none';
      }
    }
  }

  document.addEventListener( 'DOMContentLoaded', function() {
    updateClassicContentVisibility();

    document.addEventListener( 'change', function( event ) {
      if (
        event.target &&
        (
          event.target.id === 'pdt_content_visibility_mode' ||
          event.target.id === 'pdt_content_visibility_denied_behavior' ||
          event.target.id === 'content_visibility_default_denied_behavior'
        )
      ) {
        updateClassicContentVisibility();
      }
    } );
  } );
}() );
