use v6;
use Data::Dump::Tree;
use URI;
use LibCurl::Easy;
use JSON::Fast;
use RakuDoc::Citations;
use experimental :rakuast;
my %config;
my %citations =
    categories => %(
        cited => SetHash.new,
        uncited => SetHash.new
    ),
    data => %()
;
my @warnings;

my $content = $=finish;
#method gen-citation($ast, %config, $type, $level, $numerate) {
    # Allocate unique IDs for entries that lack them...
#    my $content = $ast.paragraphs.Str.join.trim;
#    my %citations := $!current.citations;
#    my @warnings := $*prs.warnings;
    # Categorize data...
    my @categories = (%config<category> // []).values;
    # create new category
    @categories.map({ %citations<categories>{$_} = SetHash.new unless %citations<categories>{$_}:exists });
    # Does the citation block load data from elsewhere???
    with %config<load> {
        # Loading external content while also specifying internal content is confusing...
        if $content ~~ /\S/ {
            @warnings.push: '=citation blocks with :load<URL> and in-document data are better written as two separate blocks'
#                    ~ " in block ｢{ $!scoped-data.last-starter }｣ with heading ｢{ $!scoped-data.last-title }｣.")
        }
        my $external-content;
        when m/ ^ file ':' / {
            my URI $f-uri .= new($_);
            if $f-uri.path.Str.IO ~~ :e & :f {
                $external-content = $f-uri.path.Str.IO.slurp;
            }
            else {
                my $error = "No file found at ｢$f-uri｣";
                @warnings.push($error)
            }
        }
        default {
            # Load and convert external data to internal Raku format, categorizing as well...
            my LibCurl::Easy $curl .= new(:URL($_), :followlocation, :failonerror );
            try {
                $external-content = $curl.perform.content;
            }
            if $! {
                my $error = "URL ｢$_｣ caused LibCurl Exception, response code ｢{ $curl.response-code }｣ with error ｢{ $curl.error }｣";
                @warnings.push($error)
            }
        }
        if $external-content ~~ / \S / {
            #                for convert-to-CSL-Raku($external-content) -> $entry {
            #                    $entry<id> //= $entry<DOI> // "Missing-ID-{$noIDcount++}";
            #                    %citations<categories>{ @categories }».set($entry<id>);
            #                    %citations<data>{ $entry<id> } = $entry.&convert-dates;
            #                }
            for convert-to-id-cls($external-content, @warnings ) -> ($id, $cls) {
                %citations<categories>{ @categories }».set($id);
                %citations<data>{$id} = $cls
            }
        }
        else {
            @warnings.push: "Could not load citation data from %config<load>"
#                    ~ " in block ｢{ $!scoped-data.last-starter }｣ with heading ｢{ $!scoped-data.last-title }｣.")
        }
    }
    # Convert citation block data to CSL data that will be used by citation and store it under its ID (or one we made up)...
    if $content ~~ /\S/ {
        for convert-to-id-cls($content, @warnings ) -> ($id, $cls) {
            %citations<categories>{ @categories }».set($id);
            %citations<data>{$id} = $cls
        }
    }
#}
ddt %citations, :title<Citations>;
ddt @warnings, :title<Warnings>;
say 'cited keys: ', %citations<categories><cited>.keys;

say 'uncited keys: ', %citations<categories><uncited>.keys;

=finish
PMID- 39876543
    STAT- Publisher
    DA  - 2025/08/15
    TI  - The Semantic Elegance of Raku Grammars.
    BTI - The Semantic Elegance of Raku Grammars
    AU  - Mercurio S
    LA  - eng
    PT  - Book
    PB  - Butterfly Press
    SN  - 978-1-2345-9876-0