use std::process::Command;

use crate::{BrowserOpener, PayPluginError};

pub struct DefaultBrowserOpener;

impl BrowserOpener for DefaultBrowserOpener {
    fn open(&self, url: &str) -> Result<(), PayPluginError> {
        let mut command = if cfg!(target_os = "macos") {
            let mut c = Command::new("open");
            c.arg(url);
            c
        } else if cfg!(target_os = "windows") {
            let mut c = Command::new("cmd");
            c.args(["/C", "start", "", url]);
            c
        } else {
            let mut c = Command::new("xdg-open");
            c.arg(url);
            c
        };
        command
            .status()
            .map_err(|e| PayPluginError::Storage(e.to_string()))
            .map(|_| ())
    }
}
