/*
 * @Author: sjl
 * @Date: 2023-05-07 20:15:47
 * @Descripttion:
 */
import type { GlobEnvConfig } from '/#/config';

import { warn } from '/@/utils/log';
import pkg from '../../package.json';
import { getConfigFileName } from '../../build/getConfigFileName';

export function getCommonStoragePrefix() {
  const { VITE_GLOB_APP_SHORT_NAME } = getAppEnvConfig();
  return `${VITE_GLOB_APP_SHORT_NAME}__${getEnv()}`.toUpperCase();
}

// Generate cache key according to version
export function getStorageShortName() {
  return `${getCommonStoragePrefix()}${`__${pkg.version}`}__`.toUpperCase();
}

export function getAppEnvConfig() {
  const ENV_NAME = getConfigFileName(import.meta.env);
  const ENV = (import.meta.env.DEV
    ? (import.meta.env as unknown as GlobEnvConfig)
    : window[ENV_NAME as any]) as unknown as GlobEnvConfig;

  const {
    VITE_GLOB_APP_TITLE,
    VITE_GLOB_API_URL,
    VITE_GLOB_APP_SHORT_NAME,
    VITE_GLOB_API_URL_PREFIX,
    VITE_GLOB_UPLOAD_URL,
    VITE_GLOB_CONFIGURATION,
    VITE_GLOB_LIGONG_PAGE,
    VITE_GLOB_WEB_SOCKET,
    VITE_GLOB_CONTENT_SECURITY_POLICY,
    VITE_GLOB_ALARM_NOTIFY_POLLING_INTERVAL_TIME,
    VITE_GLOB_ALARM_NOTIFY_DURATION,
    VITE_GLOB_LARGE_DESIGNER,
    VITE_GLOB_BIG_SCREEN,
    VITE_GLOB_LIGONG_ADMIN,
    VITE_GIS_API,
    VITE_GLOB_DEBUGGER_API,
    VITE_GLOB_BASE_IMG,
    VITE_GLOB_BASE_IMITATE,
    VITE_GLOB_IS_IDC,
  } = ENV;

  if (!/^[a-zA-Z\_]*$/.test(VITE_GLOB_APP_SHORT_NAME)) {
    warn(
      `VITE_GLOB_APP_SHORT_NAME Variables can only be characters/underscores, please modify in the environment variables and re-running.`
    );
  }

  return {
    VITE_GLOB_APP_TITLE,
    VITE_GLOB_API_URL,
    VITE_GLOB_APP_SHORT_NAME,
    VITE_GLOB_API_URL_PREFIX,
    VITE_GLOB_UPLOAD_URL,
    VITE_GLOB_CONFIGURATION,
    VITE_GLOB_LIGONG_PAGE,
    VITE_GLOB_WEB_SOCKET,
    VITE_GLOB_CONTENT_SECURITY_POLICY,
    VITE_GLOB_ALARM_NOTIFY_POLLING_INTERVAL_TIME,
    VITE_GLOB_ALARM_NOTIFY_DURATION,
    VITE_GLOB_LARGE_DESIGNER,
    VITE_GLOB_BIG_SCREEN,
    VITE_GLOB_LIGONG_ADMIN,
    VITE_GIS_API,
    VITE_GLOB_DEBUGGER_API,
    VITE_GLOB_BASE_IMG,
    VITE_GLOB_BASE_IMITATE,
    VITE_GLOB_IS_IDC,
  };
}

/**
 * @description: Development mode
 */
export const devMode = 'development';

/**
 * @description: Production mode
 */
export const prodMode = 'production';

/**
 * @description: Get environment variables
 * @returns:
 * @example:
 */
export function getEnv(): string {
  return import.meta.env.MODE;
}

/**
 * @description: Is it a development mode
 * @returns:
 * @example:
 */
export function isDevMode(): boolean {
  return import.meta.env.DEV;
}

/**
 * @description: Is it a production mode
 * @returns:
 * @example:
 */
export function isProdMode(): boolean {
  return import.meta.env.PROD;
}
