<script
    src="https://cdn.cookielaw.org/scripttemplates/otSDKStub.js"
    data-document-language="true"
    type="text/javascript"
    charset="UTF-8"
    data-domain-script="c938af5b-8612-421f-8d15-740c329a09bc{% if current_school.properties.sandbox %}-test{% endif %}"></script>

<script type="text/javascript">
  !(function () {
    var analytics = (window.analytics = window.analytics || []);
    if (!analytics.initialize)
      if (analytics.invoked)
        window.console &&
        console.error &&
        console.error("Segment snippet included twice.");
      else {
        analytics.invoked = !0;
        analytics.methods = [
          "trackSubmit",
          "trackClick",
          "trackLink",
          "trackForm",
          "pageview",
          "identify",
          "reset",
          "group",
          "track",
          "ready",
          "alias",
          "debug",
          "page",
          "once",
          "off",
          "on",
          "addSourceMiddleware",
          "addIntegrationMiddleware",
          "setAnonymousId",
          "addDestinationMiddleware",
        ];
        analytics.factory = function (e) {
          return function () {
            var t = Array.prototype.slice.call(arguments);
            t.unshift(e);
            analytics.push(t);
            return analytics;
          };
        };
        for (var e = 0; e < analytics.methods.length; e++) {
          var key = analytics.methods[e];
          analytics[key] = analytics.factory(key);
        }
        analytics.load = function (key, e) {
          var t = document.createElement("script");
          t.type = "text/javascript";
          t.async = !0;
          t.src =
            "https://cdn.segment.com/analytics.js/v1/" +
            key +
            "/analytics.min.js";
          var n = document.getElementsByTagName("script")[0];
          n.parentNode.insertBefore(t, n);
          analytics._loadOptions = e;
        };
        analytics._writeKey = "Toe2jCBJvPgwWBT0GvOylUgge4ZHeEyc"; //this will change between PROD and PPR versions. Not final key.
        analytics.SNIPPET_VERSION = "4.15.3";
      }
  })();

  var gtm_middleware = function({payload, next, integration}) {
    if (payload.obj.messageId) {
      payload.obj.properties.messageId = payload.obj.messageId;
    }
    next(payload);
  };

  window.analytics.addDestinationMiddleware('Google Tag Manager',[gtm_middleware]);
</script>

<script>
  // Read Google Analytics cookie values and format them
  function getCookie(name) {
    const match = document.cookie.match(new RegExp(`(^| )${name}=([^;]+)`));
    return Array.isArray(match) && match.length > 1 ? match[2] : "";
  }
  ("use strict");
  function _slicedToArray(t, e) {
    return (
      _arrayWithHoles(t) || _iterableToArrayLimit(t, e) || _nonIterableRest()
    );
  }
  function _nonIterableRest() {
    throw new TypeError("Invalid attempt to destructure non-iterable instance");
  }
  function _iterableToArrayLimit(t, e) {
    var r = [],
      n = !0,
      i = !1,
      o = void 0;
    try {
      for (
        var a, l = t[Symbol.iterator]();
        !(n = (a = l.next()).done) && (r.push(a.value), !e || r.length !== e);
        n = !0
      );
    } catch (t) {
      (i = !0), (o = t);
    } finally {
      try {
        n || null == l.return || l.return();
      } finally {
        if (i) throw o;
      }
    }
    return r;
  }
  function _arrayWithHoles(t) {
    if (Array.isArray(t)) return t;
  }
  function getGoogleAnalyticsClients() {
    try {
      return ga && "function" == typeof ga.getAll ? ga.getAll() : [];
    } catch (t) {
      return [];
    }
  }
  function formatGACookieValue(t) {
    if (!t) return "";
    var e = _slicedToArray("".concat(t).split("."), 4),
      r = e[2],
      n = e[3];
    return r && n ? "".concat(r, ".").concat(n) : "";
  }
  function getAnalyticsValues() {
    var t = getGoogleAnalyticsClients(),
      e = "",
      r = "";
    return (
      (r = t.length
        ? ((e = t[0].get("clientId")), t[0].get("_gid"))
        : ((e = formatGACookieValue(getCookie("_ga"))),
          formatGACookieValue(getCookie("_gid")))),
        { ga_client_id: e, gid: r }
    );
  }
</script>
<script type="text/javascript">
  function OptanonWrapper() {
    (function loadSegmentTemplate() {
      if (typeof window.analytics.load !== "function") {
        return;
      }
      var groups = window.OnetrustActiveGroups || "C0001";
      if (!groups.includes("C0002")) {
        return;
      }
      //write key within load call also would need updated.
      window.analytics.load("Toe2jCBJvPgwWBT0GvOylUgge4ZHeEyc", {
        integrations: {
          // Performance
          "Google Tag Manager": groups.includes("C0002")
        },
      });
      {% if current_person.signed_in? %}
        {% if current_school.sso_active? %}
          var email = '{{ current_person.email }}' || null
          var user_id = '{{ current_person.id }}' || null
          var sso_uid = '{{ current_person.sso_uid }}' || null
        {% else %}
          var email = '{{ current_person.email }}' || null
          var user_id = '{{ current_person.id }}' || null
          var sso_uid = null
        {% endif %}
      {% else %}
        var email = null
        var user_id = null
        var sso_uid = null
      {% endif %}
      
      analytics.identify({
        "traits":{
          "northpass_email": email ,
          "northpass_identifier": user_id,
          "pd_sso" : sso_uid
        },
      });
      analytics.page(document.title, getAnalyticsValues());
    })();
  }
</script>

{% comment %}
<style>
  #onetrust-consent-sdk *:focus {
    outline: none !important;
  }
  #onetrust-banner-sdk .banner_logo {
    background-image: url("https://s3.amazonaws.com/static.northpass.com/pipedrive/cookie.svg");
    height: 158px;
    background-repeat: no-repeat;
    background-position: center;
    display: block !important;
  }
  #onetrust-policy-text {
    margin-top: 40px !important;
    color: var(--black-digital) !important;
    font-size: 18px !important;
    line-height: 30px !important;
  }
  #onetrust-button-group-parent {
    margin-top: 15px !important;
  }
  /* button-2 */
  #onetrust-pc-btn-handler, .save-preference-btn-handler {
    background: #E1E1FF !important;
    color: #413D99 !important;
    border: none !important;
    padding: 10px 20px !important;
    font-size: 18px !important;
    line-height: 21px !important;
    font-weight: 700 !important;
    border-radius: 4px !important;
    text-align: center !important;
    cursor: pointer !important;
    text-decoration: none !important;
    width: fit-content !important;
  }
  #onetrust-pc-btn-handler:hover, .save-preference-btn-handler:hover {
    background: #C4C2FF !important;
    color: #413D99 !important;
    opacity: 1 !important;
  }
  /* button-1 */
  #onetrust-accept-btn-handler, #accept-recommended-btn-handler {
    background: var(--purple) !important;
    color: #FFF !important;
    border: none !important;
    padding: 10px 20px !important;
    font-size: 18px !important;
    line-height: 21px !important;
    font-weight: 700 !important;
    border-radius: 4px !important;
    text-align: center !important;
    width: fit-content !important;
    cursor: pointer !important;
    text-decoration: none !important;
  }
  #onetrust-accept-btn-handler:hover, #accept-recommended-btn-handler:hover{
    background: #413D99 !important;
    color: white !important;
    opacity: 1 !important;
  }
  .banner-actions-container {
    width: fit-content !important;
  }
  #onetrust-button-group {
    display: flex !important;
    justify-content: space-evenly;
    flex-direction: row-reverse;
  }
  #ot-pc-title {
    font-weight: 700 !important;
    font-size: 18px !important;
    line-height: 30px !important;
    color: var(--black-digital) !important;
    margin-top: 40px !important;
    margin-bottom: 18px !important;
  }
  #ot-pc-desc {
    font-size: 14px !important;
    line-height: 25px !important;
    color: var(--black-digital) !important;
  }
  .privacy-notice-link {
    color: #0070D6 !important;
    text-decoration: none !important;
  }
  #ot-category-title {
    font-weight: 700 !important;
    font-size: 18px !important;
    line-height: 30px !important;
    color: var(--black-digital) !important;
  }
  .ot-cat-header {
    line-height: 25px !important;
    color: var(--black-digital) !important;
    margin-top: -8px !important;
    margin-left: 10px !important;
    font-size: 16px !important;
    font-weight: 700 !important;
  }
  .ot-switch-nob {
    background: #DADDE1 !important;
    height: 23px !important;
    border: none !important;
  }
  #onetrust-pc-sdk .ot-switch-nob::before {
    bottom: 3px !important;
    background-color: #FFF !important;
    height: 17px !important;
    width: 17px !important;
    left: 3px !important;
  }
  .category-switch-handler:checked ~ .ot-switch .ot-switch-nob {
      background: var(--green) !important;
  }
  .ot-always-active {
    color: var(--black-digital) !important;
    font-weight: 400 !important;
    font-size: 14px !important;
  }
  .ot-pc-header {
    height: 0 !important;
    position: relative !important;
    padding: 0 !important;
  }
  #ot-pc-content {
    top: 0 !important;
  }
  #onetrust-consent-sdk #onetrust-pc-sdk .ot-tgl input:focus + .ot-switch {
    outline: none !important;
  }
  .ot-pc-logo {
    display: none !important;
  }
  .ot-acc-grpcntr.ot-acc-txt {
    background: none !important;
    font-size: 16px !important;
  }
  .ot-acc-grpdesc.ot-category-desc {
    color: var(--black-digital) !important;
    font-size: 16px !important;
  }
  .ot-plus-minus {
    background: #f4f5f6 !important;
    border-radius: 50% !important;
    height: 24px !important;
    width: 24px !important;
  }
  .ot-plus-minus > span {
    display: none !important;
  }
  .ot-plus-minus::after {
    display: block;
    position: relative;
    content: '';
    height: 24px;
    width: 24px;
    top: 1px;
    background: url(https://s3.amazonaws.com/static.northpass.com/pipedrive/cookie-caret.svg) no-repeat center;
    transition: rotate 0.7s;
  }
  button[aria-expanded="true"] ~ .ot-acc-hdr .ot-plus-minus::after {
    rotate: -180deg;
  }
</style>
{% endcomment %}

{% if current_person.signed_in? and current_school.properties.sandbox == false %}
  <script>
    let permissionCookie = ''
    function validateCookieProperty() {
      getCookie('OptanonConsent').split('&').forEach(x => {
        if (x.startsWith('groups='))
          permissionCookie = decodeURIComponent(x.split('=')[1])
      })
      if (permissionCookie !== '{{ current_person.properties.cookie_permissions }}') {
        const xhr = new XMLHttpRequest();
        {% if current_school.sso_active? %}
          var url = 'https://www.workato.com/webhooks/rest/997192d2-6726-4f22-afbc-8cf021019896/pipedrive-sso-change-cookie-value'
        {% else %}
          var url = 'https://www.workato.com/webhooks/rest/4a75076f-e7be-472b-b661-319f34ee9d24/pipedrive-open-change-cookie-value'
        {% endif %}
        xhr.open('POST', url);
        xhr.setRequestHeader('Content-Type', 'application/json');
        xhr.send(JSON.stringify({
          "personUUID": '{{ current_person.id }}',
          "newCookie": permissionCookie
        }));
      }
    }
    validateCookieProperty();
  </script>
{% endif %}
