/**
 * Office Context Detection Utility (Global JavaScript version)
 *
 * This file provides global functions for Office context detection
 * that can be used both in index.html and Angular components
 */

window.OfficeContextDetector = {
  /**
   * Detects if the application is running in Office Add-in context
   * @returns {boolean} True if in Office Add-in context, false otherwise
   */
  isOfficeContext: function() {
    // Check for Office-specific URL parameters (in query string or hash)
    const urlParams = new URLSearchParams(window.location.search);
    const hashParams = new URLSearchParams(window.location.hash.split('?')[1] || '');
    const hasOfficeParam = urlParams.has('_host_Info') ||
                           urlParams.has('et') ||
                           urlParams.has('_host_info') ||
                           urlParams.get('app') === 'word' ||
                           urlParams.get('app') === 'excel' ||
                           urlParams.get('app') === 'office' ||
                           hashParams.has('_host_Info');

    // Check if we're in an iframe (typical for Office Add-ins)
    const isIframe = window.self !== window.top;

    // Check hostname for Office-specific domains
    const hasOfficeHost = window.location.hostname.includes('office') ||
                          window.location.hostname.includes('microsoft') ||
                          window.location.hostname.includes('sharepoint');

    // Check if we're on POA route (hash routing or path routing)
    const isPoaRoute = window.location.hash.includes('/poa') ||
                       window.location.pathname.includes('/poa');

    return hasOfficeParam || (isIframe && hasOfficeHost) || isPoaRoute;
  },

  /**
   * Gets Office context information if available
   * @returns {object} Office context details or null if not in Office
   */
  getOfficeContextInfo: function() {
    const isOffice = this.isOfficeContext();
    if (!isOffice) {
      return null;
    }

    return {
      isOffice: isOffice,
      params: new URLSearchParams(window.location.search),
      isIframe: window.self !== window.top,
      hostname: window.location.hostname
    };
  }
};

// Path-to-hash redirect for Office Add-in context.
// When Office loads the add-in with a path-based URL (e.g. /paw-ui/poa/login),
// Angular's HashLocationStrategy reads routes from the hash fragment, not the path.
// If the hash is empty, the router defaults to "/" and never reaches /poa/login.
// This sets the hash before Angular bootstraps so the router picks up the correct route.
(function() {
  if (window.OfficeContextDetector.isOfficeContext()) {
    var pathname = window.location.pathname;
    var baseHref = '/paw-ui/';
    var baseIndex = pathname.indexOf(baseHref);
    if (baseIndex >= 0) {
      var appPath = pathname.substring(baseIndex + baseHref.length);
      if (appPath && appPath.indexOf('poa') === 0 && (!window.location.hash || !window.location.hash.includes('/poa'))) {
        window.location.hash = '#/' + appPath;
      }
    }
  }
})();
