use std::path::Path;

use crate::plugin::{CleanAction, CleanTarget, Plugin, Safety};
use crate::utils;

pub struct SveltekitPlugin;

impl Plugin for SveltekitPlugin {
    fn name(&self) -> &str {
        "sveltekit"
    }

    fn description(&self) -> &str {
        "SvelteKit build cache"
    }

    fn scan_directory(&self, dir: &Path) -> Vec<CleanTarget> {
        let mut targets = Vec::new();

        let svelte_config = dir.join("svelte.config.js");
        if svelte_config.exists() {
            let svelte_kit = dir.join(".svelte-kit");
            if svelte_kit.is_dir() {
                let size = utils::dir_size(&svelte_kit);
                targets.push(CleanTarget {
                    label: format!("{}", svelte_kit.display()),
                    size_bytes: size,
                    action: CleanAction::DeletePath(svelte_kit),
                    plugin_name: self.name().to_string(),
                    safety: Safety::Recoverable,
                    explanation: "SvelteKit build cache, regenerated with `svelte-kit sync`"
                        .to_string(),
                });
            }
        }

        targets
    }

    fn markers(&self) -> &'static [&'static str] {
        &["svelte.config.js"]
    }
}
