package Statocles::App::Role::Store;
# ABSTRACT: Role for applications using files
$Statocles::App::Role::Store::VERSION = '0.064';

use Statocles::Base 'Role';
use Statocles::Page::Document;
use Statocles::Page::File;
with 'Statocles::App';


has store => (
    is => 'ro',
    isa => Store,
    required => 1,
    coerce => Store->coercion,
);


sub pages {
    my ( $self, %options ) = @_;
    my @pages;

    my @paths;
    my $iter = $self->store->find_files( include_documents => 1 );
    while ( my $path = $iter->() ) {
        push @paths, $path;
    }

    PATH:
    for my $path ( @paths ) {

        # Check for hidden files and folders
        next if $path->basename =~ /^[.]/;
        my $parent = $path->parent;
        while ( !$parent->is_rootdir ) {
            next PATH if $parent->basename =~ /^[.]/;
            $parent = $parent->parent;
        }

        if ( $self->store->is_document( $path ) ) {
            my $page_path = $path;
            $page_path =~ s{[.]\w+$}{.html};

            my %args = (
                path => $page_path,
                app => $self,
                layout => $self->site->theme->template( site => 'layout.html' ),
                document => $self->store->read_document( $path ),
            );

            push @pages, Statocles::Page::Document->new( %args );
        }
        else {
            # If there's a markdown file, don't keep the html file, since
            # we'll be building it from the markdown
            if ( $path =~ /[.]html$/ ) {
                my $doc_path = "$path";
                $doc_path =~ s/[.]html$/.markdown/;
                next if grep { $_ eq $doc_path } @paths;
            }

            push @pages, Statocles::Page::File->new(
                path => $path,
                file_path => $self->store->path->child( $path ),
            );
        }
    }

    return @pages;
}

1;

__END__

=pod

=head1 NAME

Statocles::App::Role::Store - Role for applications using files

=head1 VERSION

version 0.064

=head1 SYNOPSIS

    package MyApp;
    use Statocles::Base 'Class';
    with 'Statocles::App::Role::Store';

    around pages => sub {
        my ( $orig, $self, %options ) = @_;
        my @pages = $self->$orig( %options );

        # ... Add/remove pages

        return @pages;
    };

=head1 DESCRIPTION

This role provides some basic functionality for those applications that want
to use L<store objects|Statocles::Store> to manage content with Markdown files.

=head1 ATTRIBUTES

=head2 store

The directory path or L<store object|Statocles::Store> containing this app's
documents. Required.

=head1 METHODS

=head2 pages

    my @pages = $app->pages;

Get all the pages for this application. Markdown documents are wrapped
in L<Statocles::Page::Document> objects, and everything else is wrapped in
L<Statocles::Page::File> objects.

=head1 AUTHOR

Doug Bell <preaction@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2016 by Doug Bell.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
