@use "sass:color";

// own vars
$eccgui-color-codeeditor-background: $eccgui-color-textfield-background !default;
$eccgui-color-codeeditor-separation: $eccgui-color-separation-divider !default;
$eccgui-size-codeeditor-height: 20rem !default;
$eccgui-size-codeeditor-toolbar-height: $button-height !default;

// adjustments
// stylelint-disable selector-class-pattern
.#{$eccgui}-codeeditor {
    position: relative;
    display: flex;
    max-width: 100%;
    height: $eccgui-size-codeeditor-height;

    .cm-panels.cm-panels-bottom:has(> .#{$eccgui}-codeeditor__footer) {
        margin: 0 1px 1px;
        border-radius: 0 0 $pt-border-radius $pt-border-radius;
    }

    &__footer-content {
        box-sizing: border-box;
        display: flex;
        gap: $eccgui-size-inline-whitespace;
        align-items: center;
        justify-content: flex-end;
        padding: 0.5 * $eccgui-size-inline-whitespace;
        font-size: $eccgui-size-typo-caption;
        line-height: $eccgui-size-typo-caption-lineheight;
        color: $eccgui-color-workspace-text;

        .#{$eccgui}-icon {
            flex-shrink: 0;
        }
    }

    [class^="cm-theme"] {
        width: 100%;
    }

    &__toolbar {
        position: absolute;
        top: 1px;
        right: 1px;
        left: 1px;
        z-index: 3;
        background-color: $eccgui-color-codeeditor-background;
        border-bottom: solid 1px $eccgui-color-codeeditor-separation;
        border-radius: $pt-border-radius $pt-border-radius 0 0;
    }

    &--has-toolbar {
        .cm-scroller {
            margin-top: $eccgui-size-codeeditor-toolbar-height !important;
        }
    }

    &__preview {
        position: absolute;
        top: calc(#{$eccgui-size-codeeditor-toolbar-height} + 1px) !important;
        right: 1px;
        bottom: 1px;
        left: 1px;
        z-index: 2;
        padding: $button-padding;
        overflow-y: auto;
        background-color: $eccgui-color-codeeditor-background;
        border-radius: 0 0 $pt-border-radius $pt-border-radius;
    }

    .cm-editor {
        box-sizing: border-box;
        flex: 1;
        width: 100%;
        min-height: 0;
        background-color: $eccgui-color-codeeditor-background;
        border-radius: $pt-border-radius;

        // get them a "border" like input boxes from blueprintjs
        box-shadow: input-transition-shadow($input-shadow-color-focus), $pt-input-box-shadow;
        clip-path: unset !important; // we may check later why they set inset(0) now

        &.#{eccgui}-disabled {
            @extend .#{$ns}-input, .#{$ns}-disabled;

            padding: 0;
        }

        &:has(.cm-content[aria-readonly="true"]) {
            box-shadow: inset 0 0 0 1px $pt-divider-black;

            &:focus {
                box-shadow: input-transition-shadow($input-shadow-color-focus, true), $input-box-shadow-focus;
            }

            .cm-cursor {
                display: none;
            }
        }

        &[class*="#{$eccgui}-intent--"] {
            animation-duration: 1s;
            animation-delay: 0.5s;
        }

        @each $each-intent, $each-bgcolor in $eccgui-map-intent-bgcolors {
            &.#{eccgui}-intent--#{$each-intent} {
                background-color: eccgui-color-mix($each-bgcolor 24%, $eccgui-color-textfield-background);
                animation-name: intent-state-flash-#{$each-intent};
            }
        }

        &.#{eccgui}-intent--warning {
            @include pt-input-intent($eccgui-color-warning-text);
        }

        &.#{eccgui}-intent--success {
            @include pt-input-intent($eccgui-color-success-text);
        }

        &.#{eccgui}-intent--danger {
            @include pt-input-intent($eccgui-color-danger-text);
        }

        &.#{eccgui}-intent--primary {
            @include pt-input-intent($eccgui-color-primary);
        }

        &.#{eccgui}-intent--info {
            @include pt-input-intent($eccgui-color-info-text);
        }

        &.#{eccgui}-intent--accent {
            @include pt-input-intent($eccgui-color-accent);
        }

        &.#{eccgui}-intent--neutral {
            @include pt-input-intent($eccgui-color-workspace-text);
        }

        &.#{eccgui}-intent--edited {
            @include pt-input-intent($eccgui-color-info-text);
        }

        &.#{eccgui}-intent--removed {
            @include pt-input-intent($eccgui-color-danger-text);

            text-decoration: line-through $eccgui-color-danger-text 2px;
        }

        .cm-scroller {
            flex: 1;
            width: calc(100% - 2px);
            height: calc(100% - 2px);
            min-height: 0;

            // fix size to prevent wrong calculation of other elements
            padding: 0;
            margin: 1px;
        }

        &.cm-focused {
            --#{$eccgui}-a11y-outline-color: #{$eccgui-color-accent};
            --#{$eccgui}-a11y-outline-offset: 0;

            @extend .#{$eccgui}-a11y-focus-by-keyboard-static;

            &.#{eccgui}-intent--warning {
                --#{$eccgui}-a11y-outline-color: #{$eccgui-color-warning-text};
            }

            &.#{eccgui}-intent--success {
                --#{$eccgui}-a11y-outline-color: #{$eccgui-color-success-text};
            }

            &.#{eccgui}-intent--danger {
                --#{$eccgui}-a11y-outline-color: #{$eccgui-color-danger-text};
            }

            &.#{eccgui}-intent--primary {
                --#{$eccgui}-a11y-outline-color: #{$eccgui-color-primary};
            }

            &.#{eccgui}-intent--info {
                --#{$eccgui}-a11y-outline-color: #{$eccgui-color-info-text};
            }

            &.#{eccgui}-intent--removed {
                text-decoration: line-through $eccgui-color-danger-text 2px;
                box-shadow: input-transition-shadow($eccgui-color-danger-text, true), $input-box-shadow-focus;
            }
        }

        .CodeMirror-hscrollbar {
            bottom: 1px;
            height: $eccgui-size-inline-whitespace !important;
        }

        .CodeMirror-vscrollbar {
            top: 1px;
            right: 1px;
            width: $eccgui-size-inline-whitespace !important;
        }

        .CodeMirror-scrollbar-filler {
            right: 1px;
            bottom: 1px;
        }

        .cm-content {
            cursor: text;
            border-right-width: $eccgui-size-inline-whitespace !important;
        }

        .cm-tab {
            position: relative;

            // mark tabulator chars
            background-color: $eccgui-color-info-background;

            &::after {
                position: absolute;
                left: 10%;
                color: eccgui-color-rgba($eccgui-color-workspace-text, $eccgui-opacity-muted);
                content: "⇥";
            }
        }
    }
}

// cm-lineNumbers

.#{$prefix}--accordion__content {
    .cm-scroller,
    .cm-content,
    .cm-gutter,
    .cm-gutters,
    .cm-lineNumbers {
        box-sizing: content-box;
    }
}

// fix a few xq-light colors regarding bad contrasts

.cm-s-xq-light {
    // add other classes if necessary
    .cm-meta {
        filter: invert(1);
    }
}
// stylelint-enable selector-class-pattern
