{#- Small, context-free UI helpers. Import with: {% import '@theme/partials/macros.twig' as bk %} -#}

{% macro section_head(s) -%}
{%- if s.title or s.url or s.nav -%}
<div class="bk-section__head">
    {%- if s.title -%}
        <h2 class="bk-section__title"{% if s.id %} id="{{ s.id }}"{% endif %}>
            {%- if s.icon and s.icon != 'none' -%}
                <span class="bk-section__icon bk-tone-{{ s.tone|default('primary') }}{{ s.boxed|default(false) ? ' bk-section__icon--boxed' }}">{{ icon(s.icon, s.boxed|default(false) ? 'size-[18px]' : 'size-[22px]') }}</span>
            {%- endif -%}
            <span class="truncate">{{ s.title }}</span>
        </h2>
    {%- else -%}<span></span>{%- endif -%}
    {%- if s.url or s.nav -%}
    <div class="flex shrink-0 items-center gap-4">
        {%- if s.url %}<a class="bk-link" href="{{ s.url }}">{{ s.url_label|default(t('View all')) }}{{ icon('arrow-right', 'size-4') }}</a>{% endif -%}
        {%- if s.nav -%}
            <div class="bk-row-nav" x-cloak>
                <button type="button" class="bk-icon-btn" @click="scroll(-1)" :disabled="!canPrev" aria-label="{{ t('Scroll left') }}">{{ icon('chevron-left', 'size-4') }}</button>
                <button type="button" class="bk-icon-btn" @click="scroll(1)" :disabled="!canNext" aria-label="{{ t('Scroll right') }}">{{ icon('chevron-right', 'size-4') }}</button>
            </div>
        {%- endif -%}
    </div>
    {%- endif -%}
</div>
{%- if s.sub|default('') %}<p class="bk-section__sub">{{ s.sub }}</p>{% endif -%}
{%- endif -%}
{%- endmacro %}

{% macro auto_url(s) -%}
{#- Listing URL matching a title list's settings, or nothing when there is no such page. -#}
{%- set query = s.sort and s.sort not in ['popular', 'random'] ? { sort: s.sort } : {} -%}
{%- if s.source == 'genre' and s.genre -%}
    {{- (s.content_type ? url('listing', query|merge({ type: s.content_type, genre: s.genre })) : url('browse', query|merge({ genre: s.genre })))|raw -}}
{%- elseif s.source == 'country' and s.country -%}
    {{- (url('country', query|merge({ code: s.country|lower })))|raw -}}
{%- elseif s.source == 'year' and s.year -%}
    {{- (url('year', query|merge({ year: s.year })))|raw -}}
{%- elseif s.source in ['sort', '', null] -%}
    {{- (s.content_type ? url('listing', query|merge({ type: s.content_type })) : url('browse', query))|raw -}}
{%- endif -%}
{%- endmacro %}

{% macro track(layout, cols, cols_md, cols_sm) -%}
class="bk-track" data-layout="{{ layout|default('grid') }}" style="--cols: {{ cols|default(6) }}; --cols-md: {{ cols_md|default(4) }}; --cols-sm: {{ cols_sm|default(2) }}"{% if layout == 'carousel' %} x-ref="track" @scroll.passive="update()"{% endif %}
{%- endmacro %}

{% macro empty(icon_name, title, text) -%}
<div class="bk-empty">
    <span class="bk-empty__icon">{{ icon(icon_name|default('info')) }}</span>
    {% if title %}<p class="text-base font-semibold text-fg">{{ title }}</p>{% endif %}
    {% if text %}<p class="max-w-md text-sm">{{ text }}</p>{% endif %}
</div>
{%- endmacro %}

{% macro pagination(current, pages, query) -%}
{%- if pages > 1 -%}
{%- set current = max(1, min(current, pages)) -%}
{%- set window = [1, current - 1, current, current + 1, pages]|filter(n => n >= 1 and n <= pages)|reduce((carry, n) => n in carry ? carry : carry|merge([n]), [])|sort -%}
<nav class="bk-pagination" aria-label="{{ t('Pagination') }}">
    {%- if current > 1 %}<a href="?{{ query|merge({ page: current - 1 })|url_encode }}" rel="prev" aria-label="{{ t('Previous page') }}">{{ icon('chevron-left', 'size-4') }}</a>{% endif -%}
    {%- set last = 0 -%}
    {%- for n in window -%}
        {%- if n - last > 1 %}<span class="is-gap" aria-hidden="true">…</span>{% endif -%}
        {%- if n == current -%}<span aria-current="page">{{ n }}</span>{%- else -%}<a href="?{{ query|merge({ page: n })|url_encode }}">{{ n }}</a>{%- endif -%}
        {%- set last = n -%}
    {%- endfor -%}
    {%- if current < pages %}<a href="?{{ query|merge({ page: current + 1 })|url_encode }}" rel="next" aria-label="{{ t('Next page') }}">{{ icon('chevron-right', 'size-4') }}</a>{% endif -%}
</nav>
{%- endif -%}
{%- endmacro %}

{% macro country(code) -%}
{%- set names = { KR: 'South Korea', CN: 'China', JP: 'Japan', TW: 'Taiwan', TH: 'Thailand', HK: 'Hong Kong', PH: 'Philippines', IN: 'India', US: 'United States', GB: 'United Kingdom', CA: 'Canada', AU: 'Australia', NZ: 'New Zealand', FR: 'France', DE: 'Germany', ES: 'Spain', IT: 'Italy', MX: 'Mexico', BR: 'Brazil', AR: 'Argentina', TR: 'Turkey', ID: 'Indonesia', VN: 'Vietnam', MY: 'Malaysia', SG: 'Singapore', SE: 'Sweden', DK: 'Denmark', NO: 'Norway', FI: 'Finland', NL: 'Netherlands', BE: 'Belgium', IE: 'Ireland', PL: 'Poland', RU: 'Russia', EG: 'Egypt', NG: 'Nigeria', ZA: 'South Africa', IL: 'Israel', AE: 'United Arab Emirates', SA: 'Saudi Arabia', PK: 'Pakistan', BD: 'Bangladesh', CO: 'Colombia', CL: 'Chile', PT: 'Portugal', AT: 'Austria', CH: 'Switzerland', CZ: 'Czechia', GR: 'Greece', HU: 'Hungary', UA: 'Ukraine' } -%}
{{- names[code|upper] ?? code|upper -}}
{%- endmacro %}

{% macro demonym(code) -%}
{%- set names = { KR: 'Korean', CN: 'Chinese', JP: 'Japanese', TW: 'Taiwanese', TH: 'Thai', HK: 'Hong Kong', PH: 'Filipino', IN: 'Indian', US: 'American', GB: 'British', CA: 'Canadian', AU: 'Australian', FR: 'French', DE: 'German', ES: 'Spanish', IT: 'Italian', MX: 'Mexican', BR: 'Brazilian', TR: 'Turkish', ID: 'Indonesian', VN: 'Vietnamese', MY: 'Malaysian', SG: 'Singaporean', SE: 'Swedish', DK: 'Danish', NO: 'Norwegian' } -%}
{{- names[code|upper] ?? code|upper -}}
{%- endmacro %}

{% macro language(code) -%}
{%- set names = { en: 'English', ko: 'Korean', ja: 'Japanese', zh: 'Chinese', cn: 'Cantonese', th: 'Thai', es: 'Spanish', fr: 'French', de: 'German', it: 'Italian', pt: 'Portuguese', ru: 'Russian', hi: 'Hindi', ta: 'Tamil', te: 'Telugu', id: 'Indonesian', ms: 'Malay', tl: 'Tagalog', vi: 'Vietnamese', tr: 'Turkish', ar: 'Arabic', he: 'Hebrew', pl: 'Polish', nl: 'Dutch', sv: 'Swedish', da: 'Danish', no: 'Norwegian', fi: 'Finnish' } -%}
{{- names[code|lower] ?? code|upper -}}
{%- endmacro %}

{% macro status(value) -%}
{%- set names = { upcoming: 'Upcoming', released: 'Released', ongoing: 'Ongoing', ended: 'Finished', hiatus: 'On hiatus', cancelled: 'Cancelled' } -%}
{{- names[value] ?? value|capitalize -}}
{%- endmacro %}

{% macro format(value) -%}
{%- set names = { TV: 'TV', TV_SHORT: 'TV short', MOVIE: 'Movie', OVA: 'OVA', ONA: 'ONA', SPECIAL: 'Special', MUSIC: 'Music video', MANGA: 'Manga', MANHWA: 'Manhwa', MANHUA: 'Manhua', ONE_SHOT: 'One-shot', NOVEL: 'Novel' } -%}
{{- names[value|upper] ?? value|replace({ '_': ' ' })|capitalize -}}
{%- endmacro %}
