use super::core::{CaracalUser, UserCommand, UserError};
use nostr_sdk::client::Error as ClientError;
use nostr_sdk::prelude::*;

impl CaracalUser {
    pub async fn change_signer(
        &self,
        signer: impl NostrSigner,
    ) -> Result<(), UserError> {
        self.client.set_signer(signer).await;

        self.tx.send(UserCommand::Resubscribe).await?;

        Ok(())
    }

    pub async fn public_key(&self) -> Result<PublicKey, ClientError> {
        match self.client.signer().await {
            Ok(signer) => {
                signer.get_public_key().await.map_err(ClientError::Signer)
            }
            Err(error) => Err(error),
        }
    }
}
