<script>
  (() => {
    if (window.GobiiIdentitySignals) {
      return;
    }

    const normalizeGaClientId = (value) => {
      const normalized = (value || '').trim().replace(/^"+|"+$/g, '');
      const match = normalized.match(/^GA\d+\.\d+\.(\d+\.\d+)$/i);
      if (match) {
        return match[1];
      }
      return normalized;
    };

    const readCookie = (name) => {
      const prefix = `${name}=`;
      const cookie = document.cookie
        .split(';')
        .map((part) => part.trim())
        .find((part) => part.startsWith(prefix));
      if (!cookie) {
        return '';
      }
      return decodeURIComponent(cookie.slice(prefix.length));
    };

    const writeCookie = (name, value, maxAge) => {
      if (!value) {
        return;
      }
      let cookie = `${name}=${encodeURIComponent(value)}; Max-Age=${maxAge}; Path=/; SameSite=Lax`;
      if (window.location.protocol === 'https:') {
        cookie += '; Secure';
      }
      document.cookie = cookie;
    };

    const deleteCookie = (name) => {
      let cookie = `${name}=; Max-Age=0; Path=/; SameSite=Lax`;
      if (window.location.protocol === 'https:') {
        cookie += '; Secure';
      }
      document.cookie = cookie;
    };

    const clearStagedFpjsCookies = () => {
      deleteCookie('gobii_signup_fpjs_visitor_id');
      deleteCookie('gobii_signup_fpjs_request_id');
    };

    const withTimeout = (promise, timeoutMs, fallbackValue) => {
      let timerId = null;
      return Promise.race([
        promise,
        new Promise((resolve) => {
          timerId = window.setTimeout(() => resolve(fallbackValue), timeoutMs);
        }),
      ]).finally(() => {
        if (timerId !== null) {
          window.clearTimeout(timerId);
        }
      });
    };

    const createGaClientIdPromise = ({ measurementId, onResolved }) => new Promise((resolve) => {
      const fallback = normalizeGaClientId(readCookie('_ga'));
      if (!measurementId || typeof window.gtag !== 'function') {
        resolve(fallback);
        return;
      }

      let settled = false;
      const finish = (value) => {
        if (settled) {
          return;
        }
        settled = true;
        resolve(normalizeGaClientId(value) || fallback || '');
      };

      const timerId = window.setTimeout(() => finish(fallback), 1000);
      try {
        window.gtag('get', measurementId, 'client_id', (value) => {
          window.clearTimeout(timerId);
          finish(value);
        });
      } catch (_error) {
        window.clearTimeout(timerId);
        finish(fallback);
      }
    }).then((gaClientId) => {
      const normalizedGaClientId = normalizeGaClientId(gaClientId);
      if (typeof onResolved === 'function') {
        onResolved(normalizedGaClientId);
      }
      return normalizedGaClientId;
    });

    const createFpjsPromise = ({
      loaderUrl,
      behaviorUrl,
      timeoutMs,
      onResolved,
    }) => withTimeout(
      import(loaderUrl)
        .then(({ load, defaultEndpoint }) => {
          const loadOptions = {};
          if (behaviorUrl) {
            loadOptions.endpoint = [`${behaviorUrl}?region=us`, defaultEndpoint];
          }
          return load(loadOptions);
        })
        .then((fpAgent) => fpAgent.get())
        .then((result) => {
          if (typeof onResolved === 'function') {
            onResolved(result);
          }
          return result;
        })
        .catch(() => null),
      timeoutMs,
      null,
    );

    window.GobiiIdentitySignals = {
      clearStagedFpjsCookies,
      createFpjsPromise,
      createGaClientIdPromise,
      deleteCookie,
      normalizeGaClientId,
      readCookie,
      withTimeout,
      writeCookie,
    };
  })();
</script>
