// loaders.gl
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors

/* eslint-disable camelcase */
import type {TextureFormat} from '@loaders.gl/schema';
import type {GLTextureFormat} from '../gl-types';
import {
  GL_RGBA32F,
  GL_COMPRESSED_R11_EAC,
  GL_COMPRESSED_RED_GREEN_RGTC2_EXT,
  GL_COMPRESSED_RED_RGTC1_EXT,
  GL_COMPRESSED_RG11_EAC,
  GL_COMPRESSED_RGB8_ETC2,
  GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2,
  GL_COMPRESSED_RGB_ATC_WEBGL,
  GL_COMPRESSED_RGB_ETC1_WEBGL,
  GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG,
  GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG,
  GL_COMPRESSED_RGB_S3TC_DXT1_EXT,
  GL_COMPRESSED_RGBA8_ETC2_EAC,
  GL_COMPRESSED_RGBA_ASTC_10x10_KHR,
  GL_COMPRESSED_RGBA_ASTC_10x5_KHR,
  GL_COMPRESSED_RGBA_ASTC_10x6_KHR,
  GL_COMPRESSED_RGBA_ASTC_10x8_KHR,
  GL_COMPRESSED_RGBA_ASTC_12x10_KHR,
  GL_COMPRESSED_RGBA_ASTC_12x12_KHR,
  GL_COMPRESSED_RGBA_ASTC_4x4_KHR,
  GL_COMPRESSED_RGBA_ASTC_5x4_KHR,
  GL_COMPRESSED_RGBA_ASTC_5x5_KHR,
  GL_COMPRESSED_RGBA_ASTC_6x5_KHR,
  GL_COMPRESSED_RGBA_ASTC_6x6_KHR,
  GL_COMPRESSED_RGBA_ASTC_8x5_KHR,
  GL_COMPRESSED_RGBA_ASTC_8x6_KHR,
  GL_COMPRESSED_RGBA_ASTC_8x8_KHR,
  GL_COMPRESSED_RGBA_ATC_EXPLICIT_ALPHA_WEBGL,
  GL_COMPRESSED_RGBA_ATC_INTERPOLATED_ALPHA_WEBGL,
  GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG,
  GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG,
  GL_COMPRESSED_RGBA_S3TC_DXT1_EXT,
  GL_COMPRESSED_RGBA_S3TC_DXT3_EXT,
  GL_COMPRESSED_RGBA_S3TC_DXT5_EXT,
  GL_COMPRESSED_SIGNED_R11_EAC,
  GL_COMPRESSED_SIGNED_RED_GREEN_RGTC2_EXT,
  GL_COMPRESSED_SIGNED_RED_RGTC1_EXT,
  GL_COMPRESSED_SIGNED_RG11_EAC,
  GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR,
  GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR,
  GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR,
  GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR,
  GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR,
  GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR,
  GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR,
  GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR,
  GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR,
  GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR,
  GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR,
  GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR,
  GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR,
  GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR,
  GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC,
  GL_COMPRESSED_SRGB8_ETC2,
  GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2,
  GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT,
  GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT,
  GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT,
  GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
} from '../gl-extensions';

const WEBGL_TO_TEXTURE_FORMAT: Record<number, TextureFormat> = {
  [GL_RGBA32F]: 'rgba32float',
  [GL_COMPRESSED_RGB_S3TC_DXT1_EXT]: 'bc1-rgb-unorm-webgl',
  [GL_COMPRESSED_SRGB_S3TC_DXT1_EXT]: 'bc1-rgb-unorm-srgb-webgl',
  [GL_COMPRESSED_RGBA_S3TC_DXT1_EXT]: 'bc1-rgba-unorm',
  [GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT]: 'bc1-rgba-unorm-srgb',
  [GL_COMPRESSED_RGBA_S3TC_DXT3_EXT]: 'bc2-rgba-unorm',
  [GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT]: 'bc2-rgba-unorm-srgb',
  [GL_COMPRESSED_RGBA_S3TC_DXT5_EXT]: 'bc3-rgba-unorm',
  [GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT]: 'bc3-rgba-unorm-srgb',
  [GL_COMPRESSED_RED_RGTC1_EXT]: 'bc4-r-unorm',
  [GL_COMPRESSED_SIGNED_RED_RGTC1_EXT]: 'bc4-r-snorm',
  [GL_COMPRESSED_RED_GREEN_RGTC2_EXT]: 'bc5-rg-unorm',
  [GL_COMPRESSED_SIGNED_RED_GREEN_RGTC2_EXT]: 'bc5-rg-snorm',
  [GL_COMPRESSED_RGB8_ETC2]: 'etc2-rgb8unorm',
  [GL_COMPRESSED_SRGB8_ETC2]: 'etc2-rgb8unorm-srgb',
  [GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2]: 'etc2-rgb8a1unorm',
  [GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2]: 'etc2-rgb8a1unorm-srgb',
  [GL_COMPRESSED_RGBA8_ETC2_EAC]: 'etc2-rgba8unorm',
  [GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC]: 'etc2-rgba8unorm-srgb',
  [GL_COMPRESSED_R11_EAC]: 'eac-r11unorm',
  [GL_COMPRESSED_SIGNED_R11_EAC]: 'eac-r11snorm',
  [GL_COMPRESSED_RG11_EAC]: 'eac-rg11unorm',
  [GL_COMPRESSED_SIGNED_RG11_EAC]: 'eac-rg11snorm',
  [GL_COMPRESSED_RGBA_ASTC_4x4_KHR]: 'astc-4x4-unorm',
  [GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR]: 'astc-4x4-unorm-srgb',
  [GL_COMPRESSED_RGBA_ASTC_5x4_KHR]: 'astc-5x4-unorm',
  [GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR]: 'astc-5x4-unorm-srgb',
  [GL_COMPRESSED_RGBA_ASTC_5x5_KHR]: 'astc-5x5-unorm',
  [GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR]: 'astc-5x5-unorm-srgb',
  [GL_COMPRESSED_RGBA_ASTC_6x5_KHR]: 'astc-6x5-unorm',
  [GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR]: 'astc-6x5-unorm-srgb',
  [GL_COMPRESSED_RGBA_ASTC_6x6_KHR]: 'astc-6x6-unorm',
  [GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR]: 'astc-6x6-unorm-srgb',
  [GL_COMPRESSED_RGBA_ASTC_8x5_KHR]: 'astc-8x5-unorm',
  [GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR]: 'astc-8x5-unorm-srgb',
  [GL_COMPRESSED_RGBA_ASTC_8x6_KHR]: 'astc-8x6-unorm',
  [GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR]: 'astc-8x6-unorm-srgb',
  [GL_COMPRESSED_RGBA_ASTC_8x8_KHR]: 'astc-8x8-unorm',
  [GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR]: 'astc-8x8-unorm-srgb',
  [GL_COMPRESSED_RGBA_ASTC_10x5_KHR]: 'astc-10x5-unorm',
  [GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR]: 'astc-10x5-unorm-srgb',
  [GL_COMPRESSED_RGBA_ASTC_10x6_KHR]: 'astc-10x6-unorm',
  [GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR]: 'astc-10x6-unorm-srgb',
  [GL_COMPRESSED_RGBA_ASTC_10x8_KHR]: 'astc-10x8-unorm',
  [GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR]: 'astc-10x8-unorm-srgb',
  [GL_COMPRESSED_RGBA_ASTC_10x10_KHR]: 'astc-10x10-unorm',
  [GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR]: 'astc-10x10-unorm-srgb',
  [GL_COMPRESSED_RGBA_ASTC_12x10_KHR]: 'astc-12x10-unorm',
  [GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR]: 'astc-12x10-unorm-srgb',
  [GL_COMPRESSED_RGBA_ASTC_12x12_KHR]: 'astc-12x12-unorm',
  [GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR]: 'astc-12x12-unorm-srgb',
  [GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG]: 'pvrtc-rgb4unorm-webgl',
  [GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG]: 'pvrtc-rgba4unorm-webgl',
  [GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG]: 'pvrtc-rgb2unorm-webgl',
  [GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG]: 'pvrtc-rgba2unorm-webgl',
  [GL_COMPRESSED_RGB_ETC1_WEBGL]: 'etc1-rgb-unorm-webgl',
  [GL_COMPRESSED_RGB_ATC_WEBGL]: 'atc-rgb-unorm-webgl',
  [GL_COMPRESSED_RGBA_ATC_EXPLICIT_ALPHA_WEBGL]: 'atc-rgba-unorm-webgl',
  [GL_COMPRESSED_RGBA_ATC_INTERPOLATED_ALPHA_WEBGL]: 'atc-rgbai-unorm-webgl'
};

const TEXTURE_FORMAT_TO_WEBGL = Object.fromEntries(
  Object.entries(WEBGL_TO_TEXTURE_FORMAT).map(([format, textureFormat]) => [
    textureFormat,
    Number(format)
  ])
) as Partial<Record<TextureFormat, GLTextureFormat>>;

export function getTextureFormatFromWebGLFormat(
  format?: GLTextureFormat
): TextureFormat | undefined {
  if (format === undefined) {
    return undefined;
  }

  return WEBGL_TO_TEXTURE_FORMAT[format];
}

/**
 * Returns the WebGL/OpenGL compressed texture constant for a canonical loaders.gl texture format.
 * @param textureFormat - Canonical texture format string.
 * @returns Matching WebGL/OpenGL constant, if known.
 */
export function getWebGLFormatFromTextureFormat(
  textureFormat?: TextureFormat
): GLTextureFormat | undefined {
  if (textureFormat === undefined) {
    return undefined;
  }

  return TEXTURE_FORMAT_TO_WEBGL[textureFormat];
}
