
/**
 * 
 * 
 * 
 * 
 * 
 *  Eventually put this into npmFunctions
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 */


/***
 *    d888888b .88b  d88. d8888b.  .d88b.  d8888b. d888888b       .d88b.  d88888b d88888b d888888b  .o88b. d888888b  .d8b.  db      
 *      `88'   88'YbdP`88 88  `8D .8P  Y8. 88  `8D `~~88~~'      .8P  Y8. 88'     88'       `88'   d8P  Y8   `88'   d8' `8b 88      
 *       88    88  88  88 88oodD' 88    88 88oobY'    88         88    88 88ooo   88ooo      88    8P         88    88ooo88 88      
 *       88    88  88  88 88~~~   88    88 88`8b      88         88    88 88~~~   88~~~      88    8b         88    88~~~88 88      
 *      .88.   88  88  88 88      `8b  d8' 88 `88.    88         `8b  d8' 88      88        .88.   Y8b  d8   .88.   88   88 88booo. 
 *    Y888888P YP  YP  YP 88       `Y88P'  88   YD    YP          `Y88P'  YP      YP      Y888888P  `Y88P' Y888888P YP   YP Y88888P 
 *                                                                                                                                  
 *                                                                                                                                  
 */

import { Web, Site, } from "@pnp/sp/presets/all";

import "@pnp/sp/webs";
import "@pnp/sp/clientside-pages/web";


/***
 *    d888888b .88b  d88. d8888b.  .d88b.  d8888b. d888888b      d8b   db d8888b. .88b  d88.      d88888b db    db d8b   db  .o88b. d888888b d888888b  .d88b.  d8b   db .d8888. 
 *      `88'   88'YbdP`88 88  `8D .8P  Y8. 88  `8D `~~88~~'      888o  88 88  `8D 88'YbdP`88      88'     88    88 888o  88 d8P  Y8 `~~88~~'   `88'   .8P  Y8. 888o  88 88'  YP 
 *       88    88  88  88 88oodD' 88    88 88oobY'    88         88V8o 88 88oodD' 88  88  88      88ooo   88    88 88V8o 88 8P         88       88    88    88 88V8o 88 `8bo.   
 *       88    88  88  88 88~~~   88    88 88`8b      88         88 V8o88 88~~~   88  88  88      88~~~   88    88 88 V8o88 8b         88       88    88    88 88 V8o88   `Y8b. 
 *      .88.   88  88  88 88      `8b  d8' 88 `88.    88         88  V888 88      88  88  88      88      88b  d88 88  V888 Y8b  d8    88      .88.   `8b  d8' 88  V888 db   8D 
 *    Y888888P YP  YP  YP 88       `Y88P'  88   YD    YP         VP   V8P 88      YP  YP  YP      YP      ~Y8888P' VP   V8P  `Y88P'    YP    Y888888P  `Y88P'  VP   V8P `8888Y' 
 *                                                                                                                                                                              
 *                                                                                                                                                                              
 */

import { getHelpfullErrorV2, } from '../Logging/ErrorHandler';

import { getFullUrlFromSlashSitesUrl } from '../Strings/urlServices';  //    webURL = getFullUrlFromSlashSitesUrl( webURL );

import { IPickedWebBasic, IPickedList } from '../../Lists/IListInterfaces';

/***
 *    d888888b .88b  d88. d8888b.  .d88b.  d8888b. d888888b      .d8888. d88888b d8888b. db    db d888888b  .o88b. d88888b .d8888. 
 *      `88'   88'YbdP`88 88  `8D .8P  Y8. 88  `8D `~~88~~'      88'  YP 88'     88  `8D 88    88   `88'   d8P  Y8 88'     88'  YP 
 *       88    88  88  88 88oodD' 88    88 88oobY'    88         `8bo.   88ooooo 88oobY' Y8    8P    88    8P      88ooooo `8bo.   
 *       88    88  88  88 88~~~   88    88 88`8b      88           `Y8b. 88~~~~~ 88`8b   `8b  d8'    88    8b      88~~~~~   `Y8b. 
 *      .88.   88  88  88 88      `8b  d8' 88 `88.    88         db   8D 88.     88 `88.  `8bd8'    .88.   Y8b  d8 88.     db   8D 
 *    Y888888P YP  YP  YP 88       `Y88P'  88   YD    YP         `8888Y' Y88888P 88   YD    YP    Y888888P  `Y88P' Y88888P `8888Y' 
 *                                                                                                                                 
 *                                                                                                                                 
 */


 /***
 *    d888888b .88b  d88. d8888b.  .d88b.  d8888b. d888888b      db   db d88888b db      d8888b. d88888b d8888b. .d8888. 
 *      `88'   88'YbdP`88 88  `8D .8P  Y8. 88  `8D `~~88~~'      88   88 88'     88      88  `8D 88'     88  `8D 88'  YP 
 *       88    88  88  88 88oodD' 88    88 88oobY'    88         88ooo88 88ooooo 88      88oodD' 88ooooo 88oobY' `8bo.   
 *       88    88  88  88 88~~~   88    88 88`8b      88         88~~~88 88~~~~~ 88      88~~~   88~~~~~ 88`8b     `Y8b. 
 *      .88.   88  88  88 88      `8b  d8' 88 `88.    88         88   88 88.     88booo. 88      88.     88 `88. db   8D 
 *    Y888888P YP  YP  YP 88       `Y88P'  88   YD    YP         YP   YP Y88888P Y88888P 88      Y88888P 88   YD `8888Y' 
 *                                                                                                                       
 *                                                                                                                       
 */

 /***
 *    d888888b .88b  d88. d8888b.  .d88b.  d8888b. d888888b       .o88b.  .d88b.  .88b  d88. d8888b.  .d88b.  d8b   db d88888b d8b   db d888888b 
 *      `88'   88'YbdP`88 88  `8D .8P  Y8. 88  `8D `~~88~~'      d8P  Y8 .8P  Y8. 88'YbdP`88 88  `8D .8P  Y8. 888o  88 88'     888o  88 `~~88~~' 
 *       88    88  88  88 88oodD' 88    88 88oobY'    88         8P      88    88 88  88  88 88oodD' 88    88 88V8o 88 88ooooo 88V8o 88    88    
 *       88    88  88  88 88~~~   88    88 88`8b      88         8b      88    88 88  88  88 88~~~   88    88 88 V8o88 88~~~~~ 88 V8o88    88    
 *      .88.   88  88  88 88      `8b  d8' 88 `88.    88         Y8b  d8 `8b  d8' 88  88  88 88      `8b  d8' 88  V888 88.     88  V888    88    
 *    Y888888P YP  YP  YP 88       `Y88P'  88   YD    YP          `Y88P'  `Y88P'  YP  YP  YP 88       `Y88P'  VP   V8P Y88888P VP   V8P    YP    
 *                                                                                                                                               
 *                                                                                                                                               
 */

 /***
 *    d88888b db    db d8888b.  .d88b.  d8888b. d888888b      d888888b d8b   db d888888b d88888b d8888b. d88888b  .d8b.   .o88b. d88888b .d8888. 
 *    88'     `8b  d8' 88  `8D .8P  Y8. 88  `8D `~~88~~'        `88'   888o  88 `~~88~~' 88'     88  `8D 88'     d8' `8b d8P  Y8 88'     88'  YP 
 *    88ooooo  `8bd8'  88oodD' 88    88 88oobY'    88            88    88V8o 88    88    88ooooo 88oobY' 88ooo   88ooo88 8P      88ooooo `8bo.   
 *    88~~~~~  .dPYb.  88~~~   88    88 88`8b      88            88    88 V8o88    88    88~~~~~ 88`8b   88~~~   88~~~88 8b      88~~~~~   `Y8b. 
 *    88.     .8P  Y8. 88      `8b  d8' 88 `88.    88           .88.   88  V888    88    88.     88 `88. 88      88   88 Y8b  d8 88.     db   8D 
 *    Y88888P YP    YP 88       `Y88P'  88   YD    YP         Y888888P VP   V8P    YP    Y88888P 88   YD YP      YP   YP  `Y88P' Y88888P `8888Y' 
 *                                                                                                                                               
 *                                                                                                                                               
 */

 export async function getWebInfoIncludingUnique( webURL : string , minOrAllProps: 'min' | 'all', alertErrors: boolean, logErrors: boolean | string, BaseErrorTrace: string ) {

    webURL = getFullUrlFromSlashSitesUrl( webURL );
    let errMessage = '';
  
    const thisWebObject = Web( webURL );
    let getMinProps = 'Title,Id,Url,ServerRelativeUrl,SiteLogoUrl,Description,HasUniqueRoleAssignments';
    if ( minOrAllProps === 'all' ) { getMinProps = '*,' + getMinProps ; }
    let pickedWeb = null;
  
    let pickedWebMin : IPickedWebBasic = {
        ServerRelativeUrl: 'Site ServerRelativeUrl',
        guid: 'Site Guid',
        title: 'Site Title',
        url: 'siteURL',
        siteIcon: 'Site Icon',
        error: '',
        HasUniqueRoleAssignments: undefined,
    };

    try {
      const webbie: any = await thisWebObject.select(getMinProps).get();
  
      if ( minOrAllProps === 'min' ) {

        pickedWebMin = {
            ServerRelativeUrl: webbie.ServerRelativeUrl,
            guid: webbie.Id,
            title: webbie.Title,
            url: webbie.Url,
            siteIcon: webbie.SiteLogoUrl,
            error: '',
            HasUniqueRoleAssignments: webbie['HasUniqueRoleAssignments'],
        };
  
        pickedWeb = pickedWebMin;
  
      } else { pickedWeb = webbie; }
  
    } catch (e) {
  
      let helpfulErrorEnd = [ webURL, , null, null ].join('|') ;
      let errorTrace = logErrors !== false ? [ BaseErrorTrace , 'Failed', 'getWebInfoIncludingUnique ~ 160 ' + logErrors, helpfulErrorEnd ].join('|') : '';
      errMessage = getHelpfullErrorV2(e, alertErrors, true, errorTrace );
      pickedWeb = pickedWebMin;
      pickedWeb.error = errMessage;
   
    }
  
    return pickedWeb;
  
   }
 
   
export async function getSiteInfo( webUrl: string, alertErrors: boolean, logErrors: boolean | string, BaseErrorTrace: string   ) {
  
    webUrl = getFullUrlFromSlashSitesUrl( webUrl );

    //2022-08-29:  Changed thisSiteInstance: ISite to : any to remove compile warning.
    let thisSiteInstance: any = null;
    let errMessage = null;

  
    try {
      thisSiteInstance = Site( webUrl );
    } catch (e) {
        let helpfulErrorEnd = [ webUrl, '', '', null, null ].join('|') ;
        let errorTrace = logErrors !== false ? [ BaseErrorTrace , 'Failed', 'getSiteInfo ~ 137 ' + logErrors, helpfulErrorEnd ].join('|') : '';
        errMessage = getHelpfullErrorV2(e, alertErrors, true, errorTrace );
    }
  
    let theSite = null;

    try {
        theSite = await thisSiteInstance.get();
      } catch (e) {

        let helpfulErrorEnd = [ webUrl, '', '', null, null ].join('|');
        let errorTrace = logErrors !== false ? [ BaseErrorTrace , 'Failed', 'getSiteInfo ~ 148 ' + logErrors, helpfulErrorEnd ].join('|') : '';

        //Set alertMe = false because it was causing false positives when clicking to Site Contents from page with EasyContents on it.
        console.log('---===>>>> getSiteInfo FAILED, NO Alert');
        errMessage = getHelpfullErrorV2(e, false, true, errorTrace );
      }

    return theSite;
  
  }
