(function () {
  'use strict';

  if (window.__CUSTAUSU_DOCTOR_LAUNCHER_SAFE_READY__) {
    return;
  }

  window.__CUSTAUSU_DOCTOR_LAUNCHER_SAFE_READY__ = true;

  var FLIGHT_DURATION = 1900;
  var CLOSE_FLIGHT_DURATION = 1350;

  window.CUSTAUSU_DOCTOR_LAUNCHER_SAFE = {
    version: '1.2.1',
    lastDebug: null,
    update: casDoctorUpdateState,
    debug: function () {
      return window.CUSTAUSU_DOCTOR_LAUNCHER_SAFE.lastDebug;
    }
  };

  function casDoctorPanel() {
    return document.getElementById('custausu-section-installation-doctor');
  }

  function casDoctorClosedPanel() {
    return document.querySelector('#custausu-section-installation-doctor.custausu-doctor-panel--standby:not([open])');
  }

  function casDoctorStage() {
    return document.querySelector('.custausu-plan-doctor-stage');
  }

  function casDoctorAdminBarHeight() {
    var bar = document.getElementById('wpadminbar');
    var rect = bar ? bar.getBoundingClientRect() : null;

    return rect && rect.height ? rect.height : 0;
  }

  function casDoctorUpdateState() {
    var panel = casDoctorPanel();
    var stage = casDoctorStage();

    window.CUSTAUSU_DOCTOR_LAUNCHER_SAFE.lastDebug = {
      version: '1.2.1',
      stage: !!stage,
      panel: !!panel,
      closedPanel: !!casDoctorClosedPanel(),
      mode: 'stage-layout-no-offset'
    };

    return window.CUSTAUSU_DOCTOR_LAUNCHER_SAFE.lastDebug;
  }

  function casDoctorEaseInOut(t) {
    return t < 0.5
      ? 4 * t * t * t
      : 1 - Math.pow(-2 * t + 2, 3) / 2;
  }

  function casDoctorTargetScroll(panel) {
    var rect = panel.getBoundingClientRect();

    return Math.max(
      0,
      window.pageYOffset + rect.top - casDoctorAdminBarHeight() - 18
    );
  }

  function casDoctorClosedTargetScroll() {
    var stage = casDoctorStage();
    var rect = stage ? stage.getBoundingClientRect() : null;

    if (!rect) {
      return Math.max(0, window.pageYOffset - 320);
    }

    return Math.max(
      0,
      window.pageYOffset + rect.top - casDoctorAdminBarHeight() - 18
    );
  }

  function casDoctorMarkArrival(panel) {
    if (!panel) {
      return;
    }

    casDoctorSetHaloHidden(panel, true);

    panel.classList.remove('custausu-doctor-panel--just-opened');
    panel.classList.remove('custausu-doctor-panel--heart-beat');

    window.setTimeout(function () {
      panel.classList.add('custausu-doctor-panel--just-opened');
    }, 120);

    window.setTimeout(function () {
      panel.classList.add('custausu-doctor-panel--heart-beat');
    }, 260);

    window.setTimeout(function () {
      panel.classList.remove('custausu-doctor-panel--just-opened');
      panel.classList.remove('custausu-doctor-panel--heart-beat');
    }, 4600);
  }

  function casDoctorSetHaloHidden(panel, hidden) {
    if (!panel || !panel.classList) {
      return;
    }

    panel.classList.toggle('custausu-doctor-panel--halo-hidden', !!hidden);
  }

  function casDoctorCreateGhostFromImage(sourceImg, sourceRect) {
    if (!sourceImg || !sourceRect) {
      return null;
    }

    var ghost = sourceImg.cloneNode(true);

    ghost.className = 'custausu-doctor-fly-ghost';
    ghost.removeAttribute('id');
    ghost.setAttribute('aria-hidden', 'true');

    ghost.style.left = sourceRect.left + 'px';
    ghost.style.top = sourceRect.top + 'px';
    ghost.style.width = sourceRect.width + 'px';
    ghost.style.height = sourceRect.height + 'px';

    document.body.appendChild(ghost);

    return ghost;
  }

  function casDoctorRemoveGhost(ghost) {
    if (ghost && ghost.parentNode) {
      ghost.parentNode.removeChild(ghost);
    }
  }

  function casDoctorAnimateOpen(panel, ghost, sourceRect) {
    if (!panel) {
      casDoctorRemoveGhost(ghost);
      return;
    }

    var prefersReducedMotion = window.matchMedia &&
      window.matchMedia('(prefers-reduced-motion: reduce)').matches;

    var startScroll = window.pageYOffset;
    var targetScroll = casDoctorTargetScroll(panel);

    if (prefersReducedMotion || !ghost || !sourceRect || !window.requestAnimationFrame) {
      window.scrollTo({
        top: targetScroll,
        behavior: 'smooth'
      });

      casDoctorSetHaloHidden(panel, true);
      panel.classList.remove('custausu-doctor-panel--doctor-flying');
      casDoctorRemoveGhost(ghost);
      casDoctorMarkArrival(panel);
      casDoctorUpdateState();
      return;
    }

    var destImg = panel.querySelector('summary.custausu-doctor-summary .custausu-doctor-summary__icon-img');

    if (!destImg) {
      window.scrollTo({
        top: targetScroll,
        behavior: 'smooth'
      });

      casDoctorSetHaloHidden(panel, true);
      panel.classList.remove('custausu-doctor-panel--doctor-flying');
      casDoctorRemoveGhost(ghost);
      casDoctorMarkArrival(panel);
      casDoctorUpdateState();
      return;
    }

    var destRect = destImg.getBoundingClientRect();
    var destDocLeft = window.pageXOffset + destRect.left;
    var destDocTop = window.pageYOffset + destRect.top;

    var finalLeft = destDocLeft - window.pageXOffset;
    var finalTop = destDocTop - targetScroll;

    var start = null;

    function frame(ts) {
      if (!start) {
        start = ts;
      }

      var raw = Math.min(1, (ts - start) / FLIGHT_DURATION);
      var eased = casDoctorEaseInOut(raw);

      var currentScroll = startScroll + ((targetScroll - startScroll) * eased);
      var left = sourceRect.left + ((finalLeft - sourceRect.left) * eased);
      var top = sourceRect.top + ((finalTop - sourceRect.top) * eased);
      var width = sourceRect.width + ((destRect.width - sourceRect.width) * eased);
      var height = sourceRect.height + ((destRect.height - sourceRect.height) * eased);

      window.scrollTo(0, currentScroll);

      ghost.style.left = left + 'px';
      ghost.style.top = top + 'px';
      ghost.style.width = width + 'px';
      ghost.style.height = height + 'px';

      if (raw < 1) {
        window.requestAnimationFrame(frame);
        return;
      }

      window.scrollTo(0, targetScroll);

      casDoctorRemoveGhost(ghost);

      casDoctorSetHaloHidden(panel, true);
      panel.classList.remove('custausu-doctor-panel--doctor-flying');
      casDoctorMarkArrival(panel);
      casDoctorUpdateState();
    }

    window.requestAnimationFrame(frame);
  }

    function casDoctorAnimateClose(panel, ghost, sourceRect) {
    if (!panel) {
      casDoctorRemoveGhost(ghost);
      return;
    }

    var prefersReducedMotion = window.matchMedia &&
      window.matchMedia('(prefers-reduced-motion: reduce)').matches;

    var startScroll = window.pageYOffset;
    var targetScroll = casDoctorClosedTargetScroll();

    if (prefersReducedMotion || !ghost || !sourceRect || !window.requestAnimationFrame) {
      window.scrollTo({
        top: targetScroll,
        behavior: 'smooth'
      });

      casDoctorRemoveGhost(ghost);
      panel.classList.remove('custausu-doctor-panel--doctor-returning');
      casDoctorSetHaloHidden(panel, false);
      casDoctorUpdateState();
      return;
    }

    var destImg = panel.querySelector('summary.custausu-doctor-summary .custausu-doctor-summary__icon-img');

    if (!destImg) {
      window.scrollTo({
        top: targetScroll,
        behavior: 'smooth'
      });

      casDoctorRemoveGhost(ghost);
      panel.classList.remove('custausu-doctor-panel--doctor-returning');
      casDoctorSetHaloHidden(panel, false);
      casDoctorUpdateState();
      return;
    }

    var destRect = destImg.getBoundingClientRect();
    var destDocLeft = window.pageXOffset + destRect.left;
    var destDocTop = window.pageYOffset + destRect.top;

    var finalLeft = destDocLeft - window.pageXOffset;
    var finalTop = destDocTop - targetScroll;

    var start = null;

    function frame(ts) {
      if (!start) {
        start = ts;
      }

      var raw = Math.min(1, (ts - start) / CLOSE_FLIGHT_DURATION);
      var eased = casDoctorEaseInOut(raw);

      var currentScroll = startScroll + ((targetScroll - startScroll) * eased);
      var left = sourceRect.left + ((finalLeft - sourceRect.left) * eased);
      var top = sourceRect.top + ((finalTop - sourceRect.top) * eased);
      var width = sourceRect.width + ((destRect.width - sourceRect.width) * eased);
      var height = sourceRect.height + ((destRect.height - sourceRect.height) * eased);

      window.scrollTo(0, currentScroll);

      ghost.style.left = left + 'px';
      ghost.style.top = top + 'px';
      ghost.style.width = width + 'px';
      ghost.style.height = height + 'px';

      if (raw < 1) {
        window.requestAnimationFrame(frame);
        return;
      }

      window.scrollTo(0, targetScroll);

      casDoctorRemoveGhost(ghost);
      panel.classList.remove('custausu-doctor-panel--doctor-returning');
      casDoctorSetHaloHidden(panel, false);
      casDoctorUpdateState();
    }

    window.requestAnimationFrame(frame);
  }

  function casDoctorOpenFromLauncher(summary) {
    var panel = summary && summary.closest
      ? summary.closest('#custausu-section-installation-doctor')
      : null;

    if (!panel || panel.open) {
      return;
    }

    var img = summary.querySelector('.custausu-doctor-summary__icon-img');
    var sourceRect = img ? img.getBoundingClientRect() : null;
    var ghost = casDoctorCreateGhostFromImage(img, sourceRect);

    casDoctorSetHaloHidden(panel, true);
    panel.open = true;

    if (ghost) {
      panel.classList.add('custausu-doctor-panel--doctor-flying');
    }

    if (summary.blur) {
      summary.blur();
    }

    window.setTimeout(function () {
      casDoctorAnimateOpen(panel, ghost, sourceRect);
    }, 40);
  }

  function casDoctorCloseToLauncher(summary) {
    var panel = summary && summary.closest
      ? summary.closest('#custausu-section-installation-doctor')
      : null;

    if (!panel || !panel.open) {
      return;
    }

    var img = summary.querySelector('.custausu-doctor-summary__icon-img');
    var sourceRect = img ? img.getBoundingClientRect() : null;
    var ghost = casDoctorCreateGhostFromImage(img, sourceRect);

    panel.classList.remove('custausu-doctor-panel--just-opened');
    panel.classList.remove('custausu-doctor-panel--heart-beat');
    casDoctorSetHaloHidden(panel, true);

    if (ghost) {
      panel.classList.add('custausu-doctor-panel--doctor-returning');
    }

    panel.open = false;

    if (summary.blur) {
      summary.blur();
    }

    window.setTimeout(function () {
      casDoctorAnimateClose(panel, ghost, sourceRect);
    }, 40);
  }


  var casDoctorLastHandledHash = '';

  function casDoctorHashTargetsPanel() {
    var hash = window.location && typeof window.location.hash === 'string'
      ? window.location.hash
      : '';

    return hash === '#custausu-section-installation-doctor'
      || hash === '#cas-section-installation-doctor';
  }

  function casDoctorHandleDeepLink() {
    var hash = window.location && typeof window.location.hash === 'string'
      ? window.location.hash
      : '';

    if (!casDoctorHashTargetsPanel()) {
      casDoctorLastHandledHash = '';
      return false;
    }

    var panel = casDoctorPanel();

    if (!panel) {
      return false;
    }

    if (casDoctorLastHandledHash === hash && panel.open) {
      return true;
    }

    casDoctorLastHandledHash = hash;
    casDoctorSetHaloHidden(panel, true);
    panel.open = true;

    window.setTimeout(function () {
      var targetScroll = casDoctorTargetScroll(panel);

      if (typeof window.scrollTo === 'function') {
        try {
          window.scrollTo({
            top: targetScroll,
            behavior: 'smooth'
          });
        } catch (error) {
          window.scrollTo(0, targetScroll);
        }
      }

      casDoctorMarkArrival(panel);
      casDoctorUpdateState();
    }, 40);

    return true;
  }

  function casDoctorInitialize() {
    casDoctorUpdateState();
    casDoctorHandleDeepLink();
  }

  document.addEventListener('click', function (event) {
    var openSummary = event.target && event.target.closest
      ? event.target.closest('#custausu-section-installation-doctor.custausu-doctor-panel--standby[open] > summary.custausu-doctor-summary')
      : null;

    if (openSummary) {
      event.preventDefault();
      casDoctorCloseToLauncher(openSummary);
      return;
    }

    var closedSummary = event.target && event.target.closest
      ? event.target.closest('#custausu-section-installation-doctor.custausu-doctor-panel--standby:not([open]) > summary.custausu-doctor-summary')
      : null;

    if (!closedSummary) {
      return;
    }

    event.preventDefault();
    casDoctorOpenFromLauncher(closedSummary);
  }, false);

  document.addEventListener('keydown', function (event) {
    var key = event.key || '';

    if (key !== 'Enter' && key !== ' ' && key !== 'Spacebar') {
      return;
    }

    var openSummary = event.target && event.target.closest
      ? event.target.closest('#custausu-section-installation-doctor.custausu-doctor-panel--standby[open] > summary.custausu-doctor-summary')
      : null;

    if (openSummary) {
      event.preventDefault();
      casDoctorCloseToLauncher(openSummary);
      return;
    }

    var closedSummary = event.target && event.target.closest
      ? event.target.closest('#custausu-section-installation-doctor.custausu-doctor-panel--standby:not([open]) > summary.custausu-doctor-summary')
      : null;

    if (!closedSummary) {
      return;
    }

    event.preventDefault();
    casDoctorOpenFromLauncher(closedSummary);
  }, false);

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', casDoctorInitialize);
  } else {
    casDoctorInitialize();
  }

  window.addEventListener('hashchange', casDoctorHandleDeepLink);
  window.addEventListener('load', casDoctorUpdateState);
  window.addEventListener('resize', casDoctorUpdateState);
}());