package Email::EzPz::ListCore;

use Moo::Role;
use IO::All;
use IPC::System::Simple qw(run capture);

has list_dir => (is => 'ro', required => 1);

has ezmlm_bindir => (is => 'ro', required => 1);

requires 'sublist_type';

sub _command_path {
  my ($self, $command) = @_;
  return io->dir($self->ezmlm_bindir)->catfile("ezmlm-${command}");
}

sub _command_args {
  my ($self, $command, @args) = @_;
  return (
    $self->_command_path($command),
    $self->list_dir,
    $self->sublist_type,
    @args
  );
}

sub _call_command {
  my ($self, @cmd) = @_;
  run $self->_command_args(@cmd);
}

sub _capture_command {
  my ($self, @cmd) = @_;
  map { chomp; $_ } capture $self->_command_args(@cmd);
}

sub add_member {
  my ($self, $member) = @_;
  $self->_call_command(sub => $member);
  return $member;
}

sub remove_member {
  my ($self, $member) = @_;
  $self->_call_command(unsub => $member);
  return $member;
}

sub members {
  my ($self) = @_;
  $self->_capture_command('list');
}

1;
