// Copyright 2026 International Digital Economy Academy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

///|
// This layer mirrors naga_oil's writeback boundary: composable-module
// identifiers that naga's WGSL writer would rename are rejected before output,
// while final generated source still gets our compatibility sanitizer.
fn wgsl_writeback_identifier_is_reserved(ident : String) -> Bool {
  if ident == "" {
    return false
  }
  ident == "array" ||
  ident == "atomic" ||
  ident == "bool" ||
  ident == "f32" ||
  ident == "f16" ||
  ident == "i32" ||
  ident == "i64" ||
  ident == "mat2x2" ||
  ident == "mat2x3" ||
  ident == "mat2x4" ||
  ident == "mat3x2" ||
  ident == "mat3x3" ||
  ident == "mat3x4" ||
  ident == "mat4x2" ||
  ident == "mat4x3" ||
  ident == "mat4x4" ||
  ident == "ptr" ||
  ident == "sampler" ||
  ident == "sampler_comparison" ||
  ident == "texture_1d" ||
  ident == "texture_2d" ||
  ident == "texture_2d_array" ||
  ident == "texture_3d" ||
  ident == "texture_cube" ||
  ident == "texture_cube_array" ||
  ident == "texture_external" ||
  ident == "texture_multisampled_2d" ||
  ident == "texture_storage_1d" ||
  ident == "texture_storage_2d" ||
  ident == "texture_storage_2d_array" ||
  ident == "texture_storage_3d" ||
  ident == "texture_depth_2d" ||
  ident == "texture_depth_2d_array" ||
  ident == "texture_depth_cube" ||
  ident == "texture_depth_cube_array" ||
  ident == "texture_depth_multisampled_2d" ||
  ident == "u32" ||
  ident == "u64" ||
  ident == "vec2" ||
  ident == "vec3" ||
  ident == "vec4" ||
  ident == "alias" ||
  ident == "bitcast" ||
  ident == "break" ||
  ident == "case" ||
  ident == "const" ||
  ident == "const_assert" ||
  ident == "continue" ||
  ident == "continuing" ||
  ident == "default" ||
  ident == "diagnostic" ||
  ident == "discard" ||
  ident == "else" ||
  ident == "enable" ||
  ident == "false" ||
  ident == "fn" ||
  ident == "for" ||
  ident == "if" ||
  ident == "let" ||
  ident == "loop" ||
  ident == "override" ||
  ident == "return" ||
  ident == "static_assert" ||
  ident == "struct" ||
  ident == "switch" ||
  ident == "true" ||
  ident == "type" ||
  ident == "var" ||
  ident == "while" ||
  ident == "CompileShader" ||
  ident == "ComputeShader" ||
  ident == "DomainShader" ||
  ident == "GeometryShader" ||
  ident == "Hullshader" ||
  ident == "NULL" ||
  ident == "Self" ||
  ident == "abstract" ||
  ident == "active" ||
  ident == "alignas" ||
  ident == "alignof" ||
  ident == "as" ||
  ident == "asm" ||
  ident == "asm_fragment" ||
  ident == "async" ||
  ident == "attribute" ||
  ident == "auto" ||
  ident == "await" ||
  ident == "become" ||
  ident == "binding_array" ||
  ident == "cast" ||
  ident == "catch" ||
  ident == "class" ||
  ident == "co_await" ||
  ident == "co_return" ||
  ident == "co_yield" ||
  ident == "coherent" ||
  ident == "column_major" ||
  ident == "common" ||
  ident == "compile" ||
  ident == "compile_fragment" ||
  ident == "concept" ||
  ident == "const_cast" ||
  ident == "consteval" ||
  ident == "constexpr" ||
  ident == "constinit" ||
  ident == "crate" ||
  ident == "debugger" ||
  ident == "decltype" ||
  ident == "delete" ||
  ident == "demote" ||
  ident == "demote_to_helper" ||
  ident == "do" ||
  ident == "dynamic_cast" ||
  ident == "enum" ||
  ident == "explicit" ||
  ident == "export" ||
  ident == "extends" ||
  ident == "extern" ||
  ident == "external" ||
  ident == "fallthrough" ||
  ident == "filter" ||
  ident == "final" ||
  ident == "finally" ||
  ident == "friend" ||
  ident == "from" ||
  ident == "fxgroup" ||
  ident == "get" ||
  ident == "goto" ||
  ident == "groupshared" ||
  ident == "handle" ||
  ident == "highp" ||
  ident == "impl" ||
  ident == "implements" ||
  ident == "import" ||
  ident == "inline" ||
  ident == "in" ||
  ident == "inout" ||
  ident == "instanceof" ||
  ident == "interface" ||
  ident == "layout" ||
  ident == "lowp" ||
  ident == "macro" ||
  ident == "macro_rules" ||
  ident == "match" ||
  ident == "mediump" ||
  ident == "meta" ||
  ident == "mod" ||
  ident == "module" ||
  ident == "move" ||
  ident == "mut" ||
  ident == "mutable" ||
  ident == "namespace" ||
  ident == "new" ||
  ident == "nil" ||
  ident == "noexcept" ||
  ident == "noinline" ||
  ident == "nointerpolation" ||
  ident == "noperspective" ||
  ident == "null" ||
  ident == "nullptr" ||
  ident == "of" ||
  ident == "operator" ||
  ident == "package" ||
  ident == "packoffset" ||
  ident == "partition" ||
  ident == "pass" ||
  ident == "patch" ||
  ident == "pixelfragment" ||
  ident == "precise" ||
  ident == "precision" ||
  ident == "premerge" ||
  ident == "priv" ||
  ident == "protected" ||
  ident == "pub" ||
  ident == "public" ||
  ident == "readonly" ||
  ident == "ref" ||
  ident == "regardless" ||
  ident == "register" ||
  ident == "reinterpret_cast" ||
  ident == "requires" ||
  ident == "resource" ||
  ident == "restrict" ||
  ident == "self" ||
  ident == "set" ||
  ident == "shared" ||
  ident == "signed" ||
  ident == "sizeof" ||
  ident == "smooth" ||
  ident == "snorm" ||
  ident == "static" ||
  ident == "static_cast" ||
  ident == "std" ||
  ident == "subroutine" ||
  ident == "super" ||
  ident == "target" ||
  ident == "template" ||
  ident == "this" ||
  ident == "thread_local" ||
  ident == "throw" ||
  ident == "trait" ||
  ident == "try" ||
  ident == "typedef" ||
  ident == "typeid" ||
  ident == "typename" ||
  ident == "typeof" ||
  ident == "union" ||
  ident == "unless" ||
  ident == "unorm" ||
  ident == "unsafe" ||
  ident == "unsized" ||
  ident == "use" ||
  ident == "using" ||
  ident == "varying" ||
  ident == "virtual" ||
  ident == "volatile" ||
  ident == "wgsl" ||
  ident == "where" ||
  ident == "with" ||
  ident == "writeonly" ||
  ident == "yield"
}

///|
fn wgsl_writeback_identifier_ends_with_digit(ident : String) -> Bool {
  if ident == "" {
    return false
  }
  ident.has_suffix("0") ||
  ident.has_suffix("1") ||
  ident.has_suffix("2") ||
  ident.has_suffix("3") ||
  ident.has_suffix("4") ||
  ident.has_suffix("5") ||
  ident.has_suffix("6") ||
  ident.has_suffix("7") ||
  ident.has_suffix("8") ||
  ident.has_suffix("9")
}

///|
fn sanitize_wgsl_writeback_identifier(ident : String) -> String {
  let had_trailing_separator = ident.has_suffix("_")
  let mut out = ident
  while out.has_suffix("_") {
    out = out[:out.length() - 1].to_owned()
  }
  while starts_with_text(out, "__") {
    out = out[1:out.length()].to_owned()
  }
  if out == "" {
    out = "unnamed"
  }
  if starts_with_text(out, "_naga") {
    out = "gen_\{out}"
  }
  if wgsl_writeback_identifier_is_reserved(out) {
    out = "\{out}_wgsl"
  } else if had_trailing_separator &&
    wgsl_writeback_identifier_ends_with_digit(out) {
    out = "\{out}_"
  }
  out
}

///|
fn wgsl_writeback_identifier_requires_substitution(ident : String) -> Bool {
  ident != "" && sanitize_wgsl_writeback_identifier(ident) != ident
}

///|
fn wgsl_redirect_map_set_sanitized(
  redirect_map : @hashmap.HashMap[String, String],
  ident : String,
) -> Unit {
  if wgsl_writeback_identifier_requires_substitution(ident) {
    let sanitized = sanitize_wgsl_writeback_identifier(ident)
    if sanitized != ident {
      redirect_map.set(ident, sanitized)
    }
  }
}

///|
fn collect_wgsl_function_parameter_sanitizers(
  data : WgslGlobalDeclData,
  redirect_map : @hashmap.HashMap[String, String],
) -> Unit {
  match data {
    WgslGlobalDeclData::Function(function) =>
      for argument in function.arguments {
        wgsl_redirect_map_set_sanitized(redirect_map, argument.name)
      }
    _ => ()
  }
}

///|
fn sanitize_wgsl_writeback_identifiers(source : String) -> String {
  let graph = build_wgsl_declaration_graph(source)
  let global_redirects : @hashmap.HashMap[String, String] = @hashmap.HashMap::new()
  for block in graph.declaration_blocks {
    wgsl_redirect_map_set_sanitized(global_redirects, block.name)
  }
  if global_redirects.is_empty() {
    let mut has_parameter_sanitizers = false
    for block in graph.declaration_blocks {
      let block_redirects : @hashmap.HashMap[String, String] = @hashmap.HashMap::new()
      collect_wgsl_function_parameter_sanitizers(block.data, block_redirects)
      if !block_redirects.is_empty() {
        has_parameter_sanitizers = true
      }
    }
    if !has_parameter_sanitizers {
      return source
    }
  }
  let mut sanitized = ""
  for import_block in graph.import_blocks {
    sanitized = sanitized + import_block.text
  }
  for block in graph.declaration_blocks {
    let block_redirects : @hashmap.HashMap[String, String] = @hashmap.HashMap::new()
    for entry in global_redirects.iter() {
      let (from_name, to_name) = entry
      block_redirects.set(from_name, to_name)
    }
    collect_wgsl_function_parameter_sanitizers(block.data, block_redirects)
    sanitized = sanitized +
      rewrite_wgsl_declaration_blocks_with_redirects(
        block.text,
        block_redirects,
        rewrite_declaration_names=true,
      )
  }
  sanitized
}

///|
fn validate_wgsl_struct_member_identifiers(
  source : String,
) -> Unit raise ComposerError {
  let graph = build_wgsl_declaration_graph(source)
  for block in graph.declaration_blocks {
    match block.data {
      WgslGlobalDeclData::Struct(struct_decl) =>
        for struct_member in struct_decl.members {
          let mut allows_writeback_substitution = false
          for attr in struct_member.attributes {
            if attr.name == "blend_src" {
              allows_writeback_substitution = true
              break
            }
          }
          if !allows_writeback_substitution &&
            struct_member.name != "" &&
            wgsl_writeback_identifier_requires_substitution(struct_member.name) {
            raise ComposerError::InvalidStructMemberIdentifier(
              struct_member.name,
            )
          }
        }
      _ => ()
    }
  }
}

///|
pub fn finalize_wgsl_writeback_source(
  source : String,
) -> String raise ComposerError {
  validate_wgsl_struct_member_identifiers(source)
  sanitize_wgsl_writeback_identifiers(source)
}