package Xchat::XPI::Events;

use strict;
use warnings;

use Xchat qw<:all>;

use constant DEFAULT_DELAY => 1000;

our $VERSION = '0.04';

sub _delay_cb {
 my ($cb, @args) = @{$_[0]};

 $cb->(@args);

 return REMOVE;
}

sub delay {
 my $delay = $_[0];

 if (defined $delay) {
  $delay = int $delay;
 } else {
  $delay = DEFAULT_DELAY;
 }

 hook_timer $delay, \&_delay_cb, { data => \@_ };
}

sub _filter_cb {
 my @args      = @{$_[0]};
 my ($cb, $to) = @{$_[1]};

 if ($cb->(@args)) {
  emit_print $to, @args;
  return EAT_ALL;
 } else {
  return EAT_NONE;
 }
}

sub filter {
 hook_print $_[0], \&_filter_cb, { data => \@_ };
}

use base qw<Exporter>;

our @EXPORT         = ();
our %EXPORT_TAGS    = (
 'funcs'  => [ qw<delay filter>  ],
 'consts' => [ qw<DEFAULT_DELAY> ],
);
our @EXPORT_OK      = map { @$_ } values %EXPORT_TAGS;
$EXPORT_TAGS{'all'} = [ @EXPORT_OK ];

1;
