yazi_macro::mod_pub!(drivers);

yazi_macro::mod_flat!(adapter adapters icc image info);

use yazi_emulator::{Brand, CLOSE, EMULATOR, ESCAPE, Emulator, Mux, START, TMUX};
use yazi_shared::in_wsl;
use yazi_shim::cell::SyncCell;

pub static ADAPTOR: SyncCell<Adapter> = SyncCell::new(Adapter::Chafa);

// Image state
static SHOWN: SyncCell<Option<ratatui::layout::Rect>> = SyncCell::new(None);

// WSL support
pub static WSL: SyncCell<bool> = SyncCell::new(false);

pub fn init() -> anyhow::Result<()> {
	// WSL support
	WSL.set(in_wsl());

	// Emulator detection
	let mut emulator = Emulator::detect().unwrap_or_default();
	TMUX.set(emulator.kind.left() == Some(Brand::Tmux));

	// Tmux support
	if TMUX.get() {
		ESCAPE.set("\x1b\x1b");
		START.set("\x1bPtmux;\x1b\x1b");
		CLOSE.set("\x1b\\");
		Mux::tmux_passthrough();
		emulator = Emulator::detect().unwrap_or_default();
	}

	EMULATOR.init(emulator);
	yazi_config::init_flavor(EMULATOR.light)?;

	ADAPTOR.set(Adapter::matches(&EMULATOR));
	ADAPTOR.get().start();
	Ok(())
}
