---
import pkg from '../../../package.json';
import integrity from '../data/integrity.json';

const CDN_URL = `https://cdn.jsdelivr.net/npm/htmx.org@${pkg.version}/dist/htmx.min.js`;
const INTEGRITY = integrity.min;
---

<!-- CDN one-liner: click anywhere to copy full <script> tag -->
<button
    class="cdn-copy"
    title="Copy CDN script tag"
    data-cdn-url={CDN_URL}
    data-integrity={INTEGRITY}
    _={`on click
         set url to my @data-cdn-url
         set integrity to my @data-integrity
         set tag to '<script src="' + url + '" integrity="' + integrity + '" crossorigin="anonymous"></script>'
         writeText(tag) on navigator.clipboard
         add .icon-\\[radix-icons--check\\] to first <i/> in me
         remove .icon-\\[radix-icons--copy\\] from first <i/> in me
         set @title of me to 'Copied!'
         add @data-copied
         wait 2s
         add .icon-\\[radix-icons--copy\\] to first <i/> in me
         remove .icon-\\[radix-icons--check\\] from first <i/> in me
         set @title of me to 'Copy to clipboard'
         remove @data-copied
     end
     on copy(event)
         halt the event
         set url to my @data-cdn-url
         set integrity to my @data-integrity
         set tag to '<script src="' + url + '" integrity="' + integrity + '" crossorigin="anonymous"></script>'
         call event.clipboardData.setData('text/plain', tag)`}
>
    <!-- Script tag with truncated URL display -->
    <code
    ><span class="tag">&lt;script</span
    ><span class="attr"> src=</span
    ><span class="val">"…/htmx.min.js"</span
    ><span class="tag">&gt;&lt;/script&gt;</span
    ></code>

    <!-- Copy icon -->
    <span class="icon-wrap">
        <i class="icon-[radix-icons--copy]" aria-hidden="true"></i>
    </span>

</button>

<style>
    .cdn-copy {
        display: flex;
        align-items: center;
        border: 1px solid light-dark(var(--color-neutral-200), var(--color-neutral-800));
        border-radius: var(--radius-lg);
        background: light-dark(var(--color-white), var(--color-neutral-950));
        padding: 0.375rem 0.375rem 0.375rem 0.75rem;
        cursor: pointer;
        outline: none;
        transition: border-color 0.15s, background 0.15s;

        @media (min-width: 640px) {
            padding: 0.5rem 0.5rem 0.5rem 1rem;
        }

        @media (hover: hover) {
            &:hover {
                border-color: light-dark(var(--color-neutral-400), var(--color-neutral-600));
                background: light-dark(var(--color-neutral-50), var(--color-neutral-900));
            }
        }

        &:is(:focus-visible, :active) {
            border-color: light-dark(var(--color-neutral-400), var(--color-neutral-600));
            background: light-dark(var(--color-neutral-50), var(--color-neutral-900));
        }

        code {
            font-family: var(--font-mono);
            font-size: 0.625rem;
            line-height: 1.25rem;
            white-space: nowrap;

            @media (min-width: 640px) { font-size: 0.75rem; }
        }

        .tag { color: light-dark(var(--color-blue-600), var(--color-blue-400)); }
        .attr { color: light-dark(var(--color-neutral-600), var(--color-neutral-400)); }
        .val { color: light-dark(var(--color-emerald-600), var(--color-emerald-400)); }
    }

    .icon-wrap {
        position: relative;
        flex-shrink: 0;
        margin-left: 0.375rem;
        width: 1.5rem;
        height: 1.5rem;
        display: flex;
        align-items: center;
        justify-content: center;
        border-radius: var(--radius-sm);
        color: light-dark(var(--color-neutral-400), var(--color-neutral-600));
        transition: color 0.15s;

        @media (min-width: 640px) {
            margin-left: 0.5rem;
            width: 1.75rem;
            height: 1.75rem;
        }

        i {
            width: 0.875rem;
            height: 0.875rem;

            @media (min-width: 640px) {
                width: 1rem;
                height: 1rem;
            }
        }

        /* "Copied!" tooltip */
        &::before {
            content: 'Copied!';
            position: absolute;
            bottom: 100%;
            left: 50%;
            transform: translateX(-50%);
            margin-bottom: 0.25rem;
            padding: 0.25rem 0.5rem;
            font-size: 0.75rem;
            font-weight: 600;
            color: light-dark(white, black);
            background: light-dark(var(--color-neutral-900), var(--color-neutral-100));
            border-radius: var(--radius-sm);
            white-space: nowrap;
            opacity: 0;
            pointer-events: none;
            transition: opacity 0.15s;
        }
    }

    /* Interact: icon color */
    @media (hover: hover) {
        .cdn-copy:hover .icon-wrap {
            color: light-dark(black, white);
        }
    }

    .cdn-copy:is(:focus-visible, :active) .icon-wrap {
        color: light-dark(black, white);
    }

    /* Copied state: show tooltip */
    .cdn-copy[data-copied] .icon-wrap::before {
        opacity: 1;
    }
</style>
