import os

root_folder = "/home/kodjo/Dev/MyDocManager/src/frontend/node_modules/@sicons"

import re


def pascal_to_snake(name: str) -> str:
  """Convert a PascalCase or CamelCase string to snake_case."""
  # Insert underscore before capital letters (except the first one)
  s1 = re.sub(r'(.)([A-Z][a-z]+)', r'\1_\2', name)
  # Handle consecutive capital letters (like 'HTTPServer' -> 'http_server')
  s2 = re.sub(r'([a-z0-9])([A-Z])', r'\1_\2', s1)
  return s2.lower()


def create_icons(file, icon_folder):
  for filename in os.listdir(f"{root_folder}/{icon_folder}"):
    print("#", end='')
    if not filename.endswith(".svg"):
      continue
    
    with open(f"{root_folder}/{icon_folder}/{filename}", "r") as f_read:
      svg_content = f_read.read().strip()
      icon_name = "icon_" + pascal_to_snake(filename.split('.')[0])
      file.write(f"{icon_name} = NotStr('''{svg_content}''')\n")
  
  print("")


if __name__ == "__main__":
  for folder in ["antd", "material", "carbon", "fa", "fluent", "ionicons4", "ionicons5", "tabler"]:
    # for folder in ["antd"]:
    print(f"Processing icons for {folder}")
    with open(f"{folder}.py", "w") as f_write:
      
      # Add README.md content to the top of the file
      if os.path.exists(f"{root_folder}/{folder}/README.md"):
        with open(f"{root_folder}/{folder}/README.md", "r") as f_readme:
          for line in f_readme:
            if line.startswith("#"):
              f_write.write(line)
            else:
              f_write.write(f"# {line}")
          f_write.write("\n\n")
      
      # Add imports
      f_write.write("from fastcore.basics import NotStr\n\n")
      
      # Add icons
      create_icons(f_write, folder)
