<?php
namespace EasyChat\Library\Email;

 
final class CallableCmsMailBridge implements CmsOutboundMailBridgeInterface {
    private $code;
    private $capabilities;
    private $sender;
    public function __construct(string $code,array $capabilities,callable $sender){$this->code=preg_replace('/[^a-z0-9_\-]/i','',$code);$this->capabilities=$capabilities;$this->sender=$sender;}
    public function code(): string{return $this->code;}
    public function capabilities(): array{return $this->capabilities;}
    public function send(MailEnvelope $mail): array{
        try{$result=call_user_func($this->sender,$mail);}
        catch(\Throwable $e){return array('success'=>false,'error'=>'cms_mail_send_failed','retryable'=>true,'transport'=>'cms_mail','bridge'=>$this->code);}
        if(is_array($result))return array_merge(array('transport'=>'cms_mail','bridge'=>$this->code),$result);
        return $result===true?array('success'=>true,'external_message_id'=>$mail->messageId(),'delivery_state'=>'accepted_by_cms','transport'=>'cms_mail','bridge'=>$this->code):array('success'=>false,'error'=>'cms_mail_rejected','retryable'=>true,'transport'=>'cms_mail','bridge'=>$this->code);
    }
}
