@import 'settings';

// The value of an optional setting (false: not set) as a CSS value.
@function ct-or-none($value) {
  @if $value {
    @return $value;
  }
  @return none;
}

@mixin ct-responsive-svg-container($width: 100%, $ratio: $ct-container-ratio) {
  display: block;
  position: relative;
  width: $width;

  &:before {
    display: block;
    float: left;
    content: '';
    width: 0;
    height: 0;
    padding-bottom: $ratio * 100%;
  }

  &:after {
    content: '';
    display: table;
    clear: both;
  }

  > svg {
    display: block;
    position: absolute;
    top: 0;
    left: 0;
  }
}

@mixin ct-align-justify(
  $ct-text-align: $ct-text-align,
  $ct-text-justify: $ct-text-justify
) {
  align-items: $ct-text-align;
  justify-content: $ct-text-justify;
  // Fallback to text-align for non-flex browsers
  @if ($ct-text-justify == 'flex-start') {
    text-align: left;
  } @else if ($ct-text-justify == 'flex-end') {
    text-align: right;
  } @else {
    text-align: center;
  }
}

// The dark defaults follow the element's `color-scheme` through light-dark().
// A var() whose fallback a browser doesn't know computes to `unset`, so they
// sit in @supports and the plain declaration stays for older browsers.
@mixin ct-chart-label(
  $ct-text-color: $ct-text-color,
  $ct-text-size: $ct-text-size,
  $ct-text-line-height: $ct-text-line-height,
  $ct-text-color-dark: $ct-text-color-dark
) {
  fill: var(--ct-label-color, #{$ct-text-color});
  color: var(--ct-label-color, #{$ct-text-color});
  font-size: var(--ct-label-size, #{$ct-text-size});
  line-height: $ct-text-line-height;

  @if $ct-text-color-dark {
    @supports (color: light-dark(#000, #fff)) {
      $color: light-dark(#{$ct-text-color}, #{$ct-text-color-dark});
      fill: var(--ct-label-color, #{$color});
      color: var(--ct-label-color, #{$color});
    }
  }
}

@mixin ct-chart-grid(
  $ct-grid-color: $ct-grid-color,
  $ct-grid-width: $ct-grid-width,
  $ct-grid-dasharray: $ct-grid-dasharray,
  $ct-grid-color-dark: $ct-grid-color-dark
) {
  stroke: var(--ct-grid-color, #{$ct-grid-color});
  stroke-width: var(--ct-grid-width, #{$ct-grid-width});
  // Always emitted, so the property works; `none` is the initial value.
  stroke-dasharray: var(--ct-grid-dasharray, #{ct-or-none($ct-grid-dasharray)});

  @if $ct-grid-color-dark {
    @supports (color: light-dark(#000, #fff)) {
      stroke: var(
        --ct-grid-color,
        light-dark(#{$ct-grid-color}, #{$ct-grid-color-dark})
      );
    }
  }
}

@mixin ct-chart-point(
  $ct-point-size: $ct-point-size,
  $ct-point-shape: $ct-point-shape
) {
  stroke-width: var(--ct-point-size, #{$ct-point-size});
  stroke-linecap: var(--ct-point-shape, #{$ct-point-shape});
}

@mixin ct-chart-line(
  $ct-line-width: $ct-line-width,
  $ct-line-dasharray: $ct-line-dasharray
) {
  fill: none;
  stroke-width: var(--ct-line-width, #{$ct-line-width});
  stroke-dasharray: var(--ct-line-dasharray, #{ct-or-none($ct-line-dasharray)});
}

@mixin ct-chart-area($ct-area-opacity: $ct-area-opacity) {
  stroke: none;
  fill-opacity: var(--ct-area-opacity, #{$ct-area-opacity});
}

@mixin ct-chart-bar($ct-bar-width: $ct-bar-width) {
  fill: none;
  stroke-width: var(--ct-bar-width, #{$ct-bar-width});
}

@mixin ct-chart-donut($ct-donut-width: $ct-donut-width) {
  fill: none;
  stroke-width: var(--ct-donut-width, #{$ct-donut-width});
}

// A series class on the element or a series group around it wins over the default color.
@mixin ct-chart-band-area(
  $ct-band-color: $ct-band-color,
  $ct-band-opacity: $ct-band-opacity
) {
  stroke: none;
  fill: var(--ct-color, var(--ct-band-color, #{$ct-band-color}));
  fill-opacity: var(--ct-band-opacity, #{$ct-band-opacity});
}

// Sets the series color as `--ct-color`, which the series' elements use.
@mixin ct-chart-series-color($color) {
  --ct-color: #{$color};

  .#{$ct-class-point},
  .#{$ct-class-line},
  .#{$ct-class-bar},
  .#{$ct-class-slice-donut} {
    stroke: var(--ct-color);
  }

  .#{$ct-class-slice-pie},
  .#{$ct-class-area},
  .#{$ct-class-band-area} {
    fill: var(--ct-color);
  }
}

@mixin ct-chart(
  $ct-container-ratio: $ct-container-ratio,
  $ct-text-color: $ct-text-color,
  $ct-text-size: $ct-text-size,
  $ct-text-color-dark: $ct-text-color-dark,
  $ct-grid-color: $ct-grid-color,
  $ct-grid-color-dark: $ct-grid-color-dark,
  $ct-grid-width: $ct-grid-width,
  $ct-grid-dasharray: $ct-grid-dasharray,
  $ct-point-size: $ct-point-size,
  $ct-point-shape: $ct-point-shape,
  $ct-line-width: $ct-line-width,
  $ct-bar-width: $ct-bar-width,
  $ct-donut-width: $ct-donut-width,
  $ct-series-names: $ct-series-names,
  $ct-series-colors: $ct-series-colors
) {
  .#{$ct-class-label} {
    @include ct-chart-label(
      $ct-text-color,
      $ct-text-size,
      $ct-text-color-dark: $ct-text-color-dark
    );
  }

  .#{$ct-class-chart-line} .#{$ct-class-label},
  .#{$ct-class-chart-bar} .#{$ct-class-label},
  // HTML labels of composed charts, whose svg has no chart-type class.
  foreignObject > .#{$ct-class-label} {
    display: flex;
  }

  // A label span fills its foreignObject, the label's box. Charts drawn in
  // the page size their spans with inline styles; markup without them
  // (from renderForHydration()) gets the same box from here.
  foreignObject > .#{$ct-class-label} {
    width: 100%;
    height: 100%;
  }

  .#{$ct-class-chart-pie} .#{$ct-class-label},
  .#{$ct-class-chart-donut} .#{$ct-class-label} {
    dominant-baseline: central;
  }

  .#{$ct-class-label}.#{$ct-class-horizontal}.#{$ct-class-start} {
    @include ct-align-justify(flex-end, flex-start);
  }

  .#{$ct-class-label}.#{$ct-class-horizontal}.#{$ct-class-end} {
    @include ct-align-justify(flex-start, flex-start);
  }

  .#{$ct-class-label}.#{$ct-class-vertical}.#{$ct-class-start} {
    @include ct-align-justify(flex-end, flex-end);
  }

  .#{$ct-class-label}.#{$ct-class-vertical}.#{$ct-class-end} {
    @include ct-align-justify(flex-end, flex-start);
  }

  .#{$ct-class-chart-bar}
    .#{$ct-class-label}.#{$ct-class-horizontal}.#{$ct-class-start} {
    @include ct-align-justify(flex-end, center);
  }

  .#{$ct-class-chart-bar}
    .#{$ct-class-label}.#{$ct-class-horizontal}.#{$ct-class-end} {
    @include ct-align-justify(flex-start, center);
  }

  .#{$ct-class-chart-bar}.#{$ct-class-horizontal-bars}
    .#{$ct-class-label}.#{$ct-class-horizontal}.#{$ct-class-start} {
    @include ct-align-justify(flex-end, flex-start);
  }

  .#{$ct-class-chart-bar}.#{$ct-class-horizontal-bars}
    .#{$ct-class-label}.#{$ct-class-horizontal}.#{$ct-class-end} {
    @include ct-align-justify(flex-start, flex-start);
  }

  .#{$ct-class-chart-bar}.#{$ct-class-horizontal-bars}
    .#{$ct-class-label}.#{$ct-class-vertical}.#{$ct-class-start} {
    @include ct-align-justify(center, flex-end);
  }

  .#{$ct-class-chart-bar}.#{$ct-class-horizontal-bars}
    .#{$ct-class-label}.#{$ct-class-vertical}.#{$ct-class-end} {
    @include ct-align-justify(center, flex-start);
  }

  .#{$ct-class-grid} {
    @include ct-chart-grid(
      $ct-grid-color,
      $ct-grid-width,
      $ct-grid-dasharray,
      $ct-grid-color-dark
    );
  }

  .#{$ct-class-grid}.#{$ct-class-grid-minor} {
    $minor-dark: $ct-grid-minor-color-dark;
    @if $minor-dark == null and $ct-grid-color-dark {
      $minor-dark: rgba(255, 255, 255, 0.08);
    }

    stroke: var(--ct-grid-minor-color, #{$ct-grid-minor-color});
    stroke-width: var(--ct-grid-minor-width, #{$ct-grid-minor-width});
    stroke-dasharray: var(
      --ct-grid-minor-dasharray,
      #{ct-or-none($ct-grid-minor-dasharray)}
    );

    @if $minor-dark {
      @supports (color: light-dark(#000, #fff)) {
        stroke: var(
          --ct-grid-minor-color,
          light-dark(#{$ct-grid-minor-color}, #{$minor-dark})
        );
      }
    }
  }

  .#{$ct-class-grid-background} {
    fill: var(--ct-grid-background-fill, #{$ct-grid-background-fill});
  }

  .#{$ct-class-point} {
    @include ct-chart-point($ct-point-size, $ct-point-shape);
  }

  .#{$ct-class-line} {
    @include ct-chart-line($ct-line-width);
  }

  .#{$ct-class-area} {
    @include ct-chart-area();
  }

  .#{$ct-class-bar} {
    @include ct-chart-bar($ct-bar-width);
  }

  .#{$ct-class-slice-donut} {
    fill: none;
  }

  // Not over a slice's stroke-width attribute, which markup without inline
  // styles has for donutWidth. :where() keeps the single-class specificity.
  .#{$ct-class-slice-donut}:not(:where([stroke-width])) {
    @include ct-chart-donut($ct-donut-width);
  }

  .#{$ct-class-band-area} {
    @include ct-chart-band-area();
  }

  @if $ct-include-colored-series {
    $colors: length($ct-series-colors);

    @for $i from 0 to length($ct-series-names) {
      $name: nth($ct-series-names, $i + 1);
      // The colors repeat when there are more names than colors.
      $index: $i % $colors + 1;
      $color: nth($ct-series-colors, $index);

      // A repeated color follows the property of the series it repeats.
      @if $i >= $colors {
        $color: var(--ct-series-#{nth($ct-series-names, $index)}, #{$color});
      }

      .#{$ct-class-series}-#{$name} {
        @include ct-chart-series-color(var(--ct-series-#{$name}, #{$color}));
      }
    }
  }
}

@if $ct-include-classes {
  @include ct-chart();

  @if $ct-include-alternative-responsive-containers {
    @for $i from 0 to length($ct-scales-names) {
      .#{nth($ct-scales-names, $i + 1)} {
        @include ct-responsive-svg-container($ratio: nth($ct-scales, $i + 1));
      }
    }
  }
}
