//*************
// Fluid
//*************

$blanks: lighten($toplevel-background, 5%);

/* The amount of nestings before repeat is defined in toHtml of Fluid.ml */
$prec-colors: (#301728, #93385f, #4f3466, #9f6b99);

$ff-color: adjust-hue($purple, -40deg);

@mixin precedence($depth) {
  .precedence-#{$depth} {
    background-color: nth($prec-colors, $depth);
  }
  .fluid-category-paren .fluid-inner.precedence-#{$depth} {
    color: nth($prec-colors, $depth);
  }
}

.copy-value {
  position: absolute;
  top: -4px;
  right: -$spacing-medium;
  color: $white1;

  opacity: 0.7;

  cursor: pointer;
  &:hover {
    opacity: 0.85;
  }
}

.fluid-paren-open .fluid-inner {
  border-radius: 8px 0 0 8px;
}
.fluid-paren-close .fluid-inner {
  border-radius: 0 8px 8px 0;
}

.live-value {
  display: block;
  position: absolute;
  right: calc(#{"100% + 30px"});

  padding: 4px;
  width: max-content;
  max-width: 260px;

  white-space: pre-wrap;
  word-break: break-all;
  word-wrap: break-word;

  font-style: normal;
  font-size: $code-font-size * 0.85;
  color: $live-value-foreground;

  background-color: $live-value-background;

  .copy-value {
    @extend .copy-value;
  }

  .jump-src {
    &:hover {
      color: $blue;
      cursor: pointer;
    }

    i.fa {
      position: absolute;
      top: -2px;
      right: -$spacing-medium;
      color: $white1;

      opacity: 0.7;

      &:hover {
        opacity: 0.85;
      }
    }
  }

  .src-arrow {
    border-style: solid;
    border-color: $live-value-background;
    border-top-width: 2px;
    border-left-width: 2px;
    border-right: 0;
    border-bottom: 0;
    border-top-left-radius: 10px;

    display: block;
    width: 20px;
    height: 2em;

    position: absolute;
    left: calc(100% - 2px);
    bottom: 100%;

    &:after {
      @include font-awesome("\f0da");
      color: $live-value-background;
      font-size: 2em;
      position: absolute;
      top: -0.5em;
      right: -0.1em;
    }
  }
}

.return-value {
  position: absolute;
  top: calc(100% + 4px);
  left: -4px;

  padding: 4px;
  width: 100%;

  white-space: pre-wrap;
  word-break: break-all;
  word-wrap: break-word;

  font-style: normal;
  font-size: $code-font-size * 0.85;
  color: $live-value-foreground;

  background-color: $live-value-background;

  &.draggable {
    cursor: move;
  }

  &.refreshed {
    color: $cyan;
  }

  .copy-value {
    @extend .copy-value;
    top: 2px;
    right: 1.2em;
    margin-right: -10px;
  }

  .warning-message {
    word-break: break-word;
    @include documentationStyles();
    padding-bottom: $spacing-small;
    border-bottom: 1px dashed grey;
    margin-bottom: $spacing-small;
  }

  .value {
    position: relative;
  }
}

#fluid-status {
  position: absolute;
  overflow-wrap: anywhere;
  top: 80px;
  right: 0px;
  width: 35ch;
  z-index: 10000;
  font-size: 10pt;
  background: white;
  color: black;

  dl {
    display: flex;
    flex-wrap: wrap;
    justify-content: start;
    padding: 3px;
  }

  dt,
  dd {
    margin-top: 3px;
  }

  dt {
    flex-basis: 10.5ch;
    color: $grey1;
    &:after {
      content: ": ";
    }
  }

  dd {
    flex-basis: 16ch;
    flex-grow: 1;
    dl {
      padding: 0;
    }
    dt {
      margin: 0;
      flex-basis: 6.5ch;
    }
    dd {
      margin: 0;
      flex-basis: 20ch;
    }
    &.tokenInfo {
      flex-basis: 30ch;
      margin-left: 2ch;
    }
    &.actions {
      flex-basis: 30ch;
      margin-left: 4ch;
    }
  }
}

#app .fluid-editor {
  /* Colors from tomorrow night: https://github.com/chriskempson/tomorrow-theme */
  $black: #1d1f21;
  $background: transparent;
  $selected-background: #2d2d2d;
  $currentline: $black3;
  $selection: $grey8;
  $foreground: $white2;
  $comment: $grey8;

  $dropdown-background: lighten($selected-background, 10%);
  $dropdown-selected-background: darken($canvas-background, 10%);
  $dropdown-foreground: lighten($foreground, 10%);
  $dropdown-selected-foreground: $foreground;

  background-color: $background;

  .selected {
    background-color: $selected-background;
  }

  color: $foreground;

  .fluid-not-executed {
    filter: brightness(60%);
  }

  .fluid-code-focus {
    filter: none;
  }

  caret-color: $foreground;
  font-size: 13.33px;

  display: flex;
  flex-direction: row;
  justify-content: space-between;

  max-width: 200ch;
  width: inherit;

  &:focus {
    outline-width: 0;
  }

  padding: 0.5ch;

  font-family: monaco, monospace;

  .fluid-tokens {
    display: inline-block;
    width: inherit;
  }

  .fluid-entry {
    display: inline-block;
    white-space: pre;

    &.fluid-newline {
      white-space: nowrap;
    }

    &.selected {
      background-color: $highlight-color;
    }
  }

  .fluid-newline {
    display: block;
  }

  .in-flag {
    background-color: darken($ff-color, 40%);
  }

  .fluid-category-string,
  .fluid-match-pattern-string {
    // I don't know why this works inside &:not(.fa) and not a level up ... but
    // I tried it, since that's how we do it in _canvas.scss
    //
    // The intent here is that chars in strings should not have ligatures (see
    // https://github.com/darklang/dark/pull/1412 for screenshots).
    &:not(.fa) {
      font-family: "Fira Mono", monospace;
    }
    white-space: pre;
    color: $string-color;
  }

  .fluid-string-ml,
  .fluid-string,
  .fluid-match-pattern-string {
    background-color: #462d4c17;
  }

  // Part of the same problem, making space for it.
  .fluid-lambda-keyword {
    margin-right: 0.11px;
  }

  .fluid-match-branch-arrow,
  .fluid-lambda-symbol,
  .fluid-lambda-var,
  .fluid-lambda-arrow {
    color: $orange;
  }

  .fluid-match-branch-arrow,
  .fluid-lambda-arrow {
    // The ligature is 28px wide but we need it to be 32px to keep things lined up appropriately.
    width: 28px;
    padding-left: 2px;
    padding-right: 2px;
  }

  .fluid-placeholder {
    font-style: italic;
  }

  .fluid-blank,
  .fluid-placeholder,
  .fluid-empty {
    background-color: $blanks;

    &.cursor-on {
      background-color: lighten($blanks, 10%);
    }
  }

  .fluid-placeholder {
    color: $grey8;
    &.cursor-on {
      color: lighten($grey8, 5%);
    }
  }

  .fluid-empty {
    color: lighten($grey2, 10%);
  }

  .fluid-keyword {
    color: $green;
  }

  .fluid-fn-version {
    color: lighten($grey2, 2%) !important;
    vertical-align: bottom;
  }

  .fluid-fn-name,
  .fluid-fn-version {
    color: $white2;
    .execution-button {
      position: absolute;
      font-size: 55%;
      right: -8px;
      top: 0px;
      transition: color linear 0.1s;
      z-index: 100;

      &:hover {
        font-size: 75%;
        font-weight: strong;
        margin-top: -2px;
        cursor: pointer;
      }

      &.execution-button-unsafe {
        color: $red;
      }

      &.execution-button-unavailable {
        color: grey;
      }

      &.execution-button-needed {
        color: green;
      }

      &.execution-button-repeat {
        color: green;
      }

      &.is-executing {
        color: white;
        transition: none;

        i {
          animation: spin 0.75s infinite linear;
        }
      }
    }
  }

  .related-change {
    color: $hover-foreground;
  }

  .fluid-partial,
  .fluid-field-partial,
  .fluid-partial-right,
  .fluid-partial-left {
    color: $red;
  }

  .fluid-partial-ghost {
    color: darken($grey8, 25%);
  }

  .fluid-entry {
    position: relative;
  }

  .fluid-match-branch-arrow,
  .fluid-infix,
  .fluid-pipe-symbol,
  .fluid-lambda-arrow {
    // Fira Code renders slightly larger than Mono in some circumstances, and
    // we don't want our symbols to be taller than our text.
    font-size: 90%;
    font-family: "Fira Code", monospace;
  }

  .fluid-pipe-symbol {
    color: $blue;
    font-size: 10px;
    padding-right: 4px;
  }

  /* Will style more later. Cmd palette should look like a smaller version of omnibox
since they are essentially both commands just acting upon different things. */
  .command-palette {
    background-color: $dropdown-background;
    position: absolute;
    left: 0;
    width: max-content;
    box-shadow: 1px 1px 1px black;
    display: flex;
    flex-direction: column;
    flex-wrap: nowrap;
    align-items: stretch;
    z-index: 1000;
    margin-top: 3px;
    border: solid 1px black;
    border-top: 0px;
    border-radius: 1px 1px 3px 3px;

    #cmd-filter {
      background: transparent;
      color: $dropdown-foreground;
      margin-top: 2px;
      margin-left: 1ch;
    }

    #fluid-dropdown {
      position: relative;
      left: 0;
      background-color: transparent;
      box-shadow: 0 0 0 transparent;
      border: 0px;
      border-radius: 0px;
    }
  }

  #fluid-dropdown {
    cursor: pointer;
    background-color: $black3;
    color: $dropdown-foreground;
    z-index: 1000;
    position: absolute;
    left: -1ch;
    box-shadow: 1px 1px 1px black;
    min-width: max-content;
    width: 100%;
    max-height: 100px;
    overflow-y: scroll;
    padding: 0;
    border: 1px solid black;
    border-top: 0px;
    border-radius: 0px 0px 2px 2px;

    ul {
      margin: 0;
      padding-inline-start: 0;
      color: $dropdown-selected-foreground;
      padding-right: 5px;

      .fluid-selected {
        background-color: $highlight-color;
        color: darken(white, 5%);
      }

      li {
        list-style-type: none; // remove bullet points
        padding: 0 1ch;
        height: 16px;
        .version {
          @extend .fluid-fn-version;
        }

        &.invalid,
        &.invalid .types {
          color: lighten($grey2, 15%);
          .invalid-culprit {
            color: lighten(saturate($red, 25%), 20%);
          }

          &.fluid-selected,
          &.fluid-selected .types {
            color: darken($highlight-color, 30%);

            .invalid-culprit {
              color: lighten(saturate($red, 20%), 40%);
            }
          }
        }

        .types {
          float: right;
          margin-left: 4ch;
          text-transform: lowercase;
          font-family: "Fira Code", monospace;
          font-size: 13.33px;
          height: 16px;
          color: darken($dropdown-foreground, 15%);
        }
      }
    }
  }
}

.fluid-ast {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  width: 100%;
  position: relative;

  @include precedence(1);
  @include precedence(2);
  @include precedence(3);
  @include precedence(4);

  .debug-ast {
    padding: 4px;
    background-color: black;
    font-style: normal;
    font-size: $code-font-size * 0.85;
    white-space: pre-wrap;
    word-break: break-all;
    word-wrap: break-word;
  }
}

.fluid-secondary-editor {
  background-color: $toplevel-background;
  box-shadow: 1px 2px 2px 0 #181818;
  position: absolute;
  left: calc(100% + 20px);
  z-index: 10;

  display: flex;
  flex-direction: row;
  justify-content: space-between;
  width: max-content;
  padding: 4px;

  .ff-icon {
    position: absolute;
    color: $ff-color;
    top: -10px;
    left: -5px;
  }

  .fluid-tokens {
    min-width: 16em;
  }
}

.fluid-error-rail {
  display: none;
  position: relative;
  width: 20px;
  margin: 0 8px;

  border-left: 1px dotted lighten($black3, 20%);

  &.show {
    display: inline-block;
  }

  .error-indicator {
    position: absolute;
    left: 8px;

    width: 12px;
    height: 12px;

    i.di {
      font-size: 12px;
      opacity: 0.8;

      &.di-option-just,
      &.di-result-ok {
        color: $grey8;
      }

      &.di-option-nothing,
      &.di-result-error {
        color: lighten($red, 10%);
      }

      &:hover {
        opacity: 1;

        &:after {
          @extend .tooltip;

          position: absolute;
          left: 25px;
          top: 2px;
          font-family: "Fira Mono";
        }
      }

      &.di-option-just:hover:after {
        content: ":take-function-off-rail (x -> Just x)";
      }

      &.di-option-nothing:hover:after {
        content: ":take-function-off-rail (ErrorRail<Nothing> -> Nothing)";
      }

      &.di-result-ok:hover:after {
        content: ":take-function-off-rail (x -> Ok x)";
      }

      &.di-result-error:hover:after {
        content: ":take-function-off-rail (ErrorRail<Error msg> -> Error msg)";
      }
    }
  }
}

$error-color: saturate($red, 10%);

@keyframes blinkError {
  from {
    background-color: $error-color;
  }
  to {
    background-color: desaturate($error-color, 30%);
  }
}

@keyframes flashError {
  from {
    background-color: $error-color;
  }
  to {
    background-color: transparent;
  }
}

@keyframes flashIncomplete {
  from {
    background-color: $error-color;
  }
  to {
    background-color: lighten($blanks, 10%);
  }
}

@mixin error-tooltip {
  @extend .tooltip;
  background-color: rgba(30, 30, 30, 0.8);
  padding: 2px;
  width: max-content;
  position: absolute;
  top: -20px;
  left: -5px;
}

.toplevel.selected .fluid-error {
  border-bottom: 1px dashed $error-color;
  /* decrease height by 1px from normal to account for 1px border */
  height: 15px;
  position: relative;

  /* Show tooltip on hover */
  &.is-origin:not(.cursor-on) {
    animation: blinkError 0.3s ease-in-out infinite;

    &:hover::after {
      @include error-tooltip;
    }
  }

  /* Briefly flash red background before fading */
  &.jumped-to {
    animation-name: flashError;
    animation-delay: 0.5s;
    animation-duration: 0.3s;
    animation-iteration-count: 3;
    animation-direction: alternate;

    &.fluid-blank,
    &.fluid-empty,
    &.fluid-placeholder {
      animation-name: flashIncomplete;
    }
  }

  /* Tooltip message for sources of the incomplete */
  &.dark-incomplete.is-origin:not(.cursor-on):hover:after {
    content: "The Incomplete came from here";
  }

  /* Tooltip message for sources of the error */
  &.dark-error.is-origin:not(.cursor-on):hover:after {
    content: "The Error came from here";
  }
}
