@use "../../core/jkl";
@use "../../core/jkl/colors";

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

        &__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);
            display: grid;
            grid-template-columns: var(--jkl-file-thumbnail-width) 1fr var(
                    --jkl-file-button-width
                );
            grid-template-areas: "image text button";
            gap: var(--jkl-file-gap);
            align-items: center;
            height: fit-content;

            &__name {
                @include jkl.text-style("paragraph-small");
                grid-area: text;
                /* stylelint-disable-next-line declaration-property-value-keyword-no-deprecated */
                word-break: break-word;

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

            &__delete {
                aspect-ratio: 1;
                grid-area: button;
            }

        &__thumbnail {
            width: 100%;
            aspect-ratio: var(--jkl-file-thumbnail-aspect-ratio);
            object-fit: cover;
            max-height: var(--jkl-file-thumbnail-max-height);
            grid-area: image;
            position: relative;
            display: flex;
            align-items: center;
            justify-content: center;
            background: var(--jkl-color-background-container-low);
            border-radius: 2px;
            overflow: hidden;
            container-type: inline-size;
            anchor-name: --thumb;
            anchor-scope: all;
            outline: 1px solid color-mix(in srgb, currentColor 3%, transparent);

            &::before, &::after {
                position: absolute;
                z-index: 1;
            }

            &::after {
                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] {
                    width: 100%;
                    height: 100%;
                    object-fit: cover;
                }
            }
        }
    }

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

            .jkl-file__content {
                grid-template-columns: 1fr var(--jkl-file-button-width);
                grid-template-rows: var(--jkl-file-thumbnail-max-height) auto;
                grid-template-areas:
                    "image image"
                    "text button";
            }
        }

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

                content: attr(data-filetype);
            }
        }

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

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

        // Error state
        &[data-state="error"] {
            --bg: var(--jkl-color-functional-error);

            &,
            a,
            a:hover,
            button,
            span {
                --text-color: var(--jkl-color-text-on-alert);
                --link-color: var(--text-color);
                --jkl-color-border-action: currentColor;
            }
        }
    }

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

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