// 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_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_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_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_RGB_S3TC_DXT1_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';
import {getTextureFormatFromWebGLFormat} from './texture-format-map';

const VULKAN_TO_WEBGL_FORMAT_MAP: Record<number, GLTextureFormat> = {
  131: GL_COMPRESSED_RGB_S3TC_DXT1_EXT,
  132: GL_COMPRESSED_SRGB_S3TC_DXT1_EXT,
  133: GL_COMPRESSED_RGBA_S3TC_DXT1_EXT,
  134: GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT,
  135: GL_COMPRESSED_RGBA_S3TC_DXT3_EXT,
  136: GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT,
  137: GL_COMPRESSED_RGBA_S3TC_DXT5_EXT,
  138: GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT,
  139: GL_COMPRESSED_RED_RGTC1_EXT,
  140: GL_COMPRESSED_SIGNED_RED_RGTC1_EXT,
  141: GL_COMPRESSED_RED_GREEN_RGTC2_EXT,
  142: GL_COMPRESSED_SIGNED_RED_GREEN_RGTC2_EXT,
  147: GL_COMPRESSED_RGB8_ETC2,
  148: GL_COMPRESSED_SRGB8_ETC2,
  149: GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2,
  150: GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2,
  151: GL_COMPRESSED_RGBA8_ETC2_EAC,
  152: GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC,
  153: GL_COMPRESSED_R11_EAC,
  154: GL_COMPRESSED_SIGNED_R11_EAC,
  155: GL_COMPRESSED_RG11_EAC,
  156: GL_COMPRESSED_SIGNED_RG11_EAC,
  157: GL_COMPRESSED_RGBA_ASTC_4x4_KHR,
  158: GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR,
  159: GL_COMPRESSED_RGBA_ASTC_5x4_KHR,
  160: GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR,
  161: GL_COMPRESSED_RGBA_ASTC_5x5_KHR,
  162: GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR,
  163: GL_COMPRESSED_RGBA_ASTC_6x5_KHR,
  164: GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR,
  165: GL_COMPRESSED_RGBA_ASTC_6x6_KHR,
  166: GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR,
  167: GL_COMPRESSED_RGBA_ASTC_8x5_KHR,
  168: GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR,
  169: GL_COMPRESSED_RGBA_ASTC_8x6_KHR,
  170: GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR,
  171: GL_COMPRESSED_RGBA_ASTC_8x8_KHR,
  172: GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR,
  173: GL_COMPRESSED_RGBA_ASTC_10x5_KHR,
  174: GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR,
  175: GL_COMPRESSED_RGBA_ASTC_10x6_KHR,
  176: GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR,
  177: GL_COMPRESSED_RGBA_ASTC_10x8_KHR,
  178: GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR,
  179: GL_COMPRESSED_RGBA_ASTC_10x10_KHR,
  180: GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR,
  181: GL_COMPRESSED_RGBA_ASTC_12x10_KHR,
  182: GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR,
  183: GL_COMPRESSED_RGBA_ASTC_12x12_KHR,
  184: GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR,
  1000054000: GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG,
  1000054001: GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG,
  1000066000: GL_COMPRESSED_RGBA_ASTC_4x4_KHR,
  1000066001: GL_COMPRESSED_RGBA_ASTC_5x4_KHR,
  1000066002: GL_COMPRESSED_RGBA_ASTC_5x5_KHR,
  1000066003: GL_COMPRESSED_RGBA_ASTC_6x5_KHR,
  1000066004: GL_COMPRESSED_RGBA_ASTC_6x6_KHR,
  1000066005: GL_COMPRESSED_RGBA_ASTC_8x5_KHR,
  1000066006: GL_COMPRESSED_RGBA_ASTC_8x6_KHR,
  1000066007: GL_COMPRESSED_RGBA_ASTC_8x8_KHR,
  1000066008: GL_COMPRESSED_RGBA_ASTC_10x5_KHR,
  1000066009: GL_COMPRESSED_RGBA_ASTC_10x6_KHR,
  1000066010: GL_COMPRESSED_RGBA_ASTC_10x8_KHR,
  1000066011: GL_COMPRESSED_RGBA_ASTC_10x10_KHR,
  1000066012: GL_COMPRESSED_RGBA_ASTC_12x10_KHR,
  1000066013: GL_COMPRESSED_RGBA_ASTC_12x12_KHR
};

/**
 * Returns WebGl format based on Vulkan format
 * Vulkan to WebGl format mapping provided here http://github.khronos.org/KTX-Specification/#formatMapping
 * Vulkan name to format number mapping provided here: https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/VkFormat.html
 * @param vkFormat
 * @returns WebGL / OpenGL constant
 */
export function mapVkFormatToWebGL(vkFormat: number): GLTextureFormat | undefined {
  return VULKAN_TO_WEBGL_FORMAT_MAP[vkFormat];
}

/**
 * Returns the canonical loaders.gl texture format corresponding to a Vulkan `vkFormat` value.
 * @param vkFormat - Vulkan format constant from the KTX container.
 * @returns Canonical texture format string, if known.
 */
export function mapVkFormatToTextureFormat(vkFormat: number): TextureFormat | undefined {
  return getTextureFormatFromWebGLFormat(mapVkFormatToWebGL(vkFormat));
}
