use std::path::Path;

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

pub struct EslintPlugin;

impl Plugin for EslintPlugin {
    fn name(&self) -> &str {
        "eslint"
    }

    fn description(&self) -> &str {
        "ESLint cache files"
    }

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

        let eslintcache = dir.join(".eslintcache");
        if eslintcache.is_file() {
            let size = eslintcache
                .metadata()
                .map(|m| utils::physical_size(&m))
                .unwrap_or(0);
            targets.push(CleanTarget {
                label: format!("{}", eslintcache.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(eslintcache),
                plugin_name: self.name().to_string(),
                safety: Safety::Recoverable,
                explanation: "ESLint cache, regenerated on next lint run".to_string(),
            });
        }

        targets
    }

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