"""Compress PNGs"""

priority = -1
from tqdm import tqdm

import os
import subprocess


def compress_pngs(root_dir):
    compressed_files = []
    for dirpath, _, filenames in os.walk(root_dir):
        for filename in filenames:
            if filename.lower().endswith(".png"):
                filepath = os.path.join(dirpath, filename)
                tqdm.write(f"Сжимаю: {filepath}")
                try:
                    subprocess.run(
                        [
                            "pngquant",
                            "--force",
                            "--ext",
                            ".png",
                            "--quality=65-90",
                            filepath,
                        ],
                        check=True,
                        capture_output=True,
                    )
                    compressed_files.append(filepath)
                except subprocess.CalledProcessError as e:
                    tqdm.write(f"Ошибка при сжатии {filepath}: {e}")
    return compressed_files


def apply(config: dict) -> bool:
    files = compress_pngs("./decompiled")
    return len(files) > 0 and any(files)
