@use "../../jkl";

@layer jokul.components {
    .jkl-file {
        --jkl-file-padding: var(--jkl-unit-10);
        --jkl-file-thumbnail-max-height: var(--jkl-unit-210);
        --jkl-file-thumbnail-width: var(--jkl-unit-60);
        --jkl-file-thumbnail-aspect-ratio: 1;
        --jkl-file-gap: var(--jkl-unit-10);
        --jkl-file-button-width: var(--jkl-unit-50);
        --text-color: var(--jkl-color-text-default);
        --border: 1px solid var(--jkl-color-border-subdued);
        --border-radius: var(--jkl-border-radius-s);
        --bg: transparent;
        --transition-time: #{jkl.$motion-timing-expressive};

        &__thumbnail {
            flex-shrink: 0;
            aspect-ratio: var(--jkl-file-thumbnail-aspect-ratio);
            width: var(--jkl-file-thumbnail-width);
            height: auto;
            max-height: var(--jkl-file-thumbnail-max-height);
            position: relative;
            display: flex;
            align-items: center;
            justify-content: center;
            background: var(--jkl-color-background-page);
            border-radius: var(--jkl-border-radius-xs);
            overflow: hidden;
            anchor-name: --thumb;
            anchor-scope: all;
            outline: 1px solid color-mix(in srgb, currentColor 3%, transparent);

            &::after {
                position: absolute;
                z-index: 1;
                content: "" / "Laster opp";
                position-anchor: --thumb;
                position-area: center;
                height: 25%;
                border-radius: 1px;
                aspect-ratio: 1;
                scale: 0;
                background: var(--text-color);
                animation: spin 5s infinite forwards linear;
                transition:  display var(--transition-time) allow-discrete ease-in, scale var(--transition-time) ease-in;

                @starting-style {
                    scale: 0;
                }
            }

            img {
                opacity: 1;
                transition:  opacity var(--transition-time) ease-in-out;

                &[src] {
                    min-width: 100%;
                    min-height: 100%;
                    object-fit: cover;
                    object-position: center;
                }
            }
        }

        &__content {
            @include jkl.motion(
                "standard",
                "snappy",
                background-color,
                border-color,
                color
            );

            color: var(--text-color);
            padding: var(--jkl-file-padding);
            border: var(--border);
            border-radius: var(--border-radius);
            background: var(--bg);
            height: fit-content;

            &:not(:has(.jkl-file__thumbnail)):has(.jkl-file__button) {
                --jkl-file-padding: 0 var(--jkl-unit-10);
            }
        }

        &__info {
            flex-grow: 999;
        }

        &__name {
            /* stylelint-disable-next-line declaration-property-value-keyword-no-deprecated */
            word-break: break-word;
        }

        &__size {
            word-break: keep-all;
        }

        &__button {
            aspect-ratio: 1;
            flex-shrink: 0;
        }


        // Card view
        &[class*="card"] {
            --jkl-file-thumbnail-width: 100%;
            --jkl-file-thumbnail-aspect-ratio: 16/10;
        }

        // If the file is not an image
        &:not([data-state*="loading"]) {
            .jkl-file__thumbnail:not(:has(img[src]))::before {
                @include jkl.text-style("paragraph-small");
                position: absolute;
                z-index: 1;

                content: attr(data-filetype);
            }
        }

        // Loading state
        &[data-state="loading"]:not(:has(.jkl-file__support-label)) {
            .jkl-file__thumbnail {
                img {
                    opacity: 0;
                }

                &::after {
                    display: block;
                    scale: 1;
                }
            }
        }

        // Error state
        &[data-state="error"] {
            &,
            a,
            a:hover,
            button,
            span {
                --text-color: var(--jkl-color-error-text-default);
            }

            .jkl-file__content {
                border-color: var(--jkl-color-error-border-default);
            }
        }
    }

    @keyframes spin {
        from {
            transform: rotate(0turn);
        }

        to {
            transform: rotate(1turn);
        }
    }
}
