/*
 * ******************************************************************
 * Copyright (c) 2025 Broadcom. All Rights Reserved.
 * Broadcom Confidential. The term "Broadcom" refers to Broadcom Inc.
 * and/or its subsidiaries.
 * ******************************************************************
 */
import { HttpClient } from '@angular/common/http';
import { Injectable, Injector } from '@angular/core';
import { BootstrapDetails, HYPER_PLUGIN } from '@vcf/ng-hyper-plugin-framework';

/**
 * Populates information used by UI Plugins in Injection Token BootstrapDetails
 * and starts up the bootstrapping process of UI Plugins.
 *
 * Informaiton passed to UI Plugins include:
 * - API Root
 * - Current Organization
 * - Current Organization ID
 * - Current locale
 * - Curent scope (always 'tenant' for VCFA)
 *
 * Other type of inromation is also populated, such as:
 * - List of UI Plugins the current tenant can access
 *
 * @param http
 * @param bootstrapDetailsService
 * @returns
 */
export const hyperPluginFactory = (http: HttpClient, bootstrapDetailsService: BootstrapDetailsService, injector: Injector) => {
  /**
   * Does not load 3rd party ui services in soft tenancy mode.
   */
  return () => {
    bootstrapDetailsService.bootstrapDetails = {
      "devMode": true,
      "locale": localStorage.getItem("locale") || "en",
      "scope": window["_bootstrapDetails"]["scope"] || "tenant",
      "thirdPartyExtensions": window["_bootstrapDetails"]["thirdPartyExtensions"],
      "cell": window["_bootstrapDetails"]["cell"],
    };

    /**
     * Starts the loading of UI Plugins.
     *
     * When the `HYPER_PLUGIN` InjectionToken is used for 1st time
     * it runs a bootstrap function that starts the loading of UI Plugins.
     *
     * Angular triggers the initialization of `HYPER_PLUGIN` InjectionToken,
     * `HYPER_PLUGIN` has a factory function,
     * that factory function gets executed when you inject `HYPER_PLUGIN`.
     *
     * This mechanism offers good flexibility when to start the UI Plugin
     * bootstrap process.
     */
    injector.get(HYPER_PLUGIN);
    return;
  };
};

@Injectable()
export class BootstrapDetailsService {
  bootstrapDetails: {devMode: boolean} & BootstrapDetails = {
    devMode: true,
    cell: {
      address: '',
      organization: '',
      orgId: '',
    },
    thirdPartyExtensions: {},
    locale: '',
    scope: 'tenant',
  };
}

export interface CurrentSession {
  operatingOrg: {
    id: string;
    name: string;
  };
}

export interface PreferenceClassifier {
  classifier: string;
  name: string;
  value: string;
}
