package Podius::Component::Center;

use base 'Podius::Component::Page';

use Exception;

use Podius::Property::ComponentCollection;
use Podius::Property::VComponentCollection;

sub get_own_property_types ($) {
	return [
		[ products      => 'VComponentCollection', 'Product' ],
		[ articles      => 'VComponentCollection', 'Article' ],
		[ sections      => 'ComponentCollection', 'Section' ],
		[ product_sets  => 'ComponentCollection', 'ProductSet' ],
		[ article_sets  => 'ComponentCollection', 'ArticleSet' ],
		[ user_sets     => 'ComponentCollection', 'UserSet' ],
	];
}

sub foreach_user ($$) {
	my $self = shift;
	my $code = shift;
	my $component = shift || $self;

	$component->user_sets->foreach(sub ($) {
		@{$_->users->foreach($code)},
		@{$self->foreach_user($code, $_)};
	});
}

sub find_user_by_username ($$) {
	my $self = shift;
	my $username = shift;
	return try {
		$self->foreach_user(sub ($) {
			my $user = shift;
			return ($user->user_name || "") eq $username? ($user): ();
		})
	}->[0];
}

sub get_all_user_emails ($) {
	my $self = shift;
	return $self->foreach_user(sub ($) {
		my $user = shift;
		my $email = $user->email;
		return () unless $email;
		my $name = $user->first_name || "";
		$name .= " " . $user->middle_name if $user->middle_name;
		$name .= " " . $user->last_name if $user->last_name;
		return "$name <$email>";
	});
}

1;