use std::path::Path;

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

pub struct ParcelPlugin;

impl Plugin for ParcelPlugin {
    fn name(&self) -> &str {
        "parcel"
    }

    fn description(&self) -> &str {
        "Parcel bundler cache"
    }

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

        let parcel_cache = dir.join(".parcel-cache");
        if parcel_cache.is_dir() {
            let size = utils::dir_size(&parcel_cache);
            targets.push(CleanTarget {
                label: format!("{}", parcel_cache.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(parcel_cache),
                plugin_name: self.name().to_string(),
                safety: Safety::Recoverable,
                explanation: "Parcel build cache, regenerated on next build".to_string(),
            });
        }

        targets
    }

    fn markers(&self) -> &'static [&'static str] {
        &[".parcelrc", ".parcel-cache"]
    }
}
