// 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.

///|
priv struct WgslImportRequest {
  module_path : String
  rel_path : String
  mut import_full_module : Bool
  mut keep_requested_items : Bool
  requested_items : Array[String]
  declared_items : Array[String]
  alias_name : String
}

///|
fn find_wgsl_import_request_index(
  requests : Array[WgslImportRequest],
  module_path : String,
  rel_path : String,
  alias_name : String,
) -> Int32? {
  for i in 0.. Array[String] {
  let copied : Array[String] = []
  for item in items {
    copied.push(item)
  }
  copied
}

///|
fn wgsl_import_request_copy(request : WgslImportRequest) -> WgslImportRequest {
  {
    module_path: request.module_path,
    rel_path: request.rel_path,
    import_full_module: request.import_full_module,
    keep_requested_items: request.keep_requested_items,
    requested_items: wgsl_import_request_copy_items(request.requested_items),
    declared_items: wgsl_import_request_copy_items(request.declared_items),
    alias_name: request.alias_name,
  }
}

///|
fn wgsl_import_request_array_copy(
  requests : Array[WgslImportRequest],
) -> Array[WgslImportRequest] {
  let copied : Array[WgslImportRequest] = []
  for request in requests {
    copied.push(wgsl_import_request_copy(request))
  }
  copied
}

///|
fn wgsl_import_request_visit_key(
  importer_rel_path : String,
  request : WgslImportRequest,
) -> String {
  let mut key = "\{importer_rel_path}=>\{request.rel_path}|\{request.alias_name}"
  if request.import_full_module {
    key = "\{key}|*"
  } else {
    if request.keep_requested_items {
      key = "\{key}|explicit"
    }
    let sorted_items : Array[String] = []
    for item in request.requested_items {
      sorted_items.push(item)
    }
    sorted_items.sort()
    for item in sorted_items {
      key = "\{key}|\{item}"
    }
  }
  key
}

///|
fn wgsl_import_request_item_visit_key(
  importer_rel_path : String,
  request : WgslImportRequest,
  item : String,
) -> String {
  let explicit = if request.keep_requested_items { "explicit" } else { "item" }
  "\{importer_rel_path}=>\{request.rel_path}|\{request.alias_name}|\{explicit}|\{item}"
}

///|
fn wgsl_full_module_visit_key(rel_path : String, alias_name : String) -> String {
  "\{rel_path}|\{alias_name}|*"
}

///|
fn wgsl_imported_item_emission_key(rel_path : String, item : String) -> String {
  "\{rel_path}|item|\{item}"
}

///|
fn wgsl_path_stack_contains(paths : Array[String], rel_path : String) -> Bool {
  paths.contains(rel_path)
}

///|
fn wgsl_import_cycle_string(
  active_rel_paths : Array[String],
  repeated_rel_path : String,
) -> String {
  let cycle_paths : Array[String] = []
  let mut found_cycle_start = false
  for rel_path in active_rel_paths {
    if rel_path == repeated_rel_path {
      found_cycle_start = true
    }
    if found_cycle_start {
      cycle_paths.push(rel_path)
    }
  }
  cycle_paths.push(repeated_rel_path)
  cycle_paths.join(" -> ")
}