use lib 'lib';
use lib 'specs/support';
use BDD::Behave;
use BloginTest;
use Blogin::Layout;
use Blogin::Style;

sub urls(Int $count --> Array) {
  (1 .. $count).map({ $_ == 1 ?? '/posts' !! "/posts/page/$_" }).Array;
}

sub bar(Int $count, Int $page --> Str) {
  Blogin::Layout::ListView.new(
    page-urls   => urls($count),
    page-number => $page,
    prev-url    => ($page > 1     ?? ($page == 2 ?? '/posts' !! "/posts/page/{ $page - 1 }") !! ''),
    next-url    => ($page < $count ?? "/posts/page/{ $page + 1 }" !! ''),
  ).pagination-html;
}

describe 'the built-in numbered pagination bar', {
  it 'renders nothing for a single page', {
    expect(bar(1, 1)).to.eq('');
  }

  it 'marks the current page with aria-current', {
    expect(bar(12, 6).contains('aria-current="page">6</span>')).to.be-truthy;
  }

  it 'shows three numbers on either side of the current page', {
    my $html = bar(12, 6);
    expect($html.contains('>3</a>') && $html.contains('>9</a>')).to.be-truthy;
  }

  it 'leaves out pages beyond the three-page window', {
    my $html = bar(12, 6);
    expect($html.contains('>2</a>') || $html.contains('>10</a>')).to.be-falsy;
  }

  it 'offers first and last controls in the middle of a long run', {
    my $html = bar(12, 6);
    expect($html.contains('aria-label="First"') && $html.contains('aria-label="Last"')).to.be-truthy;
  }

  it 'drops the first and previous controls on the opening page', {
    my $html = bar(12, 1);
    expect($html.contains('aria-label="First"') || $html.contains('rel="prev"')).to.be-falsy;
  }

  it 'drops the next and last controls on the closing page', {
    my $html = bar(12, 12);
    expect($html.contains('aria-label="Last"') || $html.contains('rel="next"')).to.be-falsy;
  }

  it 'carries a stable class for the stylesheet to target', {
    expect(bar(12, 6).contains('class="blogin-pagination"')).to.be-truthy;
  }
}

describe 'the generated stylesheet spaces the pagination bar', {
  it 'gives the pagination a bottom margin by default', {
    my $css = Blogin::Style::content-css();
    expect($css.contains('.blogin-pagination') && $css.contains('margin-bottom: 1rem')).to.be-truthy;
  }
}
