use crate::service::emoji_cache::EmojiCacheError;
use crate::service::generic_id_cache::GenericIdCacheError;
use crate::service::instance_cache::RemoteInstanceCacheError;
use crate::service::instance_meta_cache::InstanceMetaCacheError;
use magnetar_model::sea_orm::DbErr;
use magnetar_model::CalckeyDbError;
use magnetar_sdk::mmm::Token;
use thiserror::Error;

pub mod drive;
pub mod emoji;
pub mod note;
pub mod notification;
pub mod user;

#[derive(Debug, Error, strum::IntoStaticStr)]
pub enum PackError {
    #[error("Database error: {0}")]
    DbError(#[from] DbErr),
    #[error("Calckey database wrapper error: {0}")]
    CalckeyDbError(#[from] CalckeyDbError),
    #[error("Data error: {0}")]
    DataError(String),
    #[error("Emoji cache error: {0}")]
    EmojiCacheError(#[from] EmojiCacheError),
    #[error("Instance cache error: {0}")]
    InstanceMetaCacheError(#[from] InstanceMetaCacheError),
    #[error("Generic cache error: {0}")]
    GenericCacheError(#[from] GenericIdCacheError),
    #[error("Remote instance cache error: {0}")]
    RemoteInstanceCacheError(#[from] RemoteInstanceCacheError),
    #[error("Deserializer error: {0}")]
    DeserializerError(#[from] serde_json::Error),
    #[error("URL parse error: {0}")]
    UrlParseError(#[from] url::ParseError),
    #[error("Parallel processing error: {0}")]
    JoinError(#[from] tokio::task::JoinError),
}

pub type PackResult<T> = Result<T, PackError>;

fn get_mm_token_emoji(token: &Token) -> Vec<String> {
    let mut v = Vec::new();
    token.walk_map_collect(
        &|t| {
            // TODO: Remote emoji
            if let Token::ShortcodeEmoji { shortcode, .. } = t {
                Some(shortcode.to_owned())
            } else {
                None
            }
        },
        &mut v,
    );
    v
}
