/* stylelint-disable declaration-no-important  */
@function rem($value) {
  @if (unitless($value)) {
    $value: $value * 1px;
  }

  @return $value / $base-font-size * 1rem;
}

@function em($value) {
  @if (unitless($value)) {
    $value: $value * 1px;
  }

  @return $value / $base-font-size * 1em;
}

// generates modifer classes in the bem syntax
@mixin helper-classes($attribute, $map) {
  @each $modifier, $value in $map {
    &--#{$modifier} {
      #{$attribute}: get-mapped-value($modifier, $map) !important;
    }
  }
}

//gets mapped px values and returns it as rem
@function get-mapped-value($key, $map) {
  @each $mapkey, $mapvalue in $map {
    @if $mapkey == $key {
      @return rem($mapvalue);
    }
  }
}

// break out of the nsw-container class
@mixin break-out($position: relative) {
  @if $position != 'relative' and $position != 'absolute' {
    @error 'NSW-DS breakout mixin allows the following values for $position: relative, absolute. You have used "#{$position}"';
  }

  width: 100vw;
  position: $position;
  left: 50%;
  right: 50%;
  margin-left: -50vw;
  margin-right: -50vw;
}

@mixin break-out-reset {
  width: auto;
  position: static;
  margin-left: auto;
  margin-right: auto;
  left: auto;
  right: auto;
}

//
// Function to create an optimized svg url
// Version: 1.0.6
@function svg-url($svg) {
  //
  // Add missing namespace
  //
  @if not str-index($svg,xmlns) {
    $svg: str-replace($svg, '<svg', '<svg xmlns="http://www.w3.org/2000/svg"');
  }
  //
  // Chunk up string in order to avoid
  // "stack level too deep" error
  //
  $encoded: '';
  $slice: 2000;
  $index: 0;
  $loops: ceil(str-length($svg)/$slice);

  @for $i from 1 through $loops {
    $chunk: str-slice($svg, $index, $index + $slice - 1);
    //
    // Encode
    //
    $chunk: str-replace($chunk, '"', '\'');
    $chunk: str-replace($chunk, '%', '%25');
    $chunk: str-replace($chunk, '#', '%23');
    $chunk: str-replace($chunk, '{', '%7B');
    $chunk: str-replace($chunk, '}', '%7D');
    $chunk: str-replace($chunk, '<', '%3C');
    $chunk: str-replace($chunk, '>', '%3E');
    //
    // The maybe list
    //
    // Keep size and compile time down
    // ... only add on documented fail
    //
    // $chunk: str-replace($chunk, '&', '%26');
    // $chunk: str-replace($chunk, '|', '%7C');
    // $chunk: str-replace($chunk, '[', '%5B');
    // $chunk: str-replace($chunk, ']', '%5D');
    // $chunk: str-replace($chunk, '^', '%5E');
    // $chunk: str-replace($chunk, '`', '%60');
    // $chunk: str-replace($chunk, ';', '%3B');
    // $chunk: str-replace($chunk, '?', '%3F');
    // $chunk: str-replace($chunk, ':', '%3A');
    // $chunk: str-replace($chunk, '@', '%40');
    // $chunk: str-replace($chunk, '=', '%3D');
    $encoded: #{$encoded}#{$chunk};
    $index: $index + $slice;
  }

  @return url('data:image/svg+xml,#{$encoded}');
}

// Background svg mixin
@mixin background-svg($id, $fill: null) {
  $new-svg: map-get($icons, $id);

  @if $fill {
    $new-svg: str-insert($new-svg, 'fill="#{$fill}" ', 6);
  }

  background-image: svg-url($new-svg);
}

// Helper function to replace characters in a string
@function str-replace($string, $search, $replace: '') {
  $index: str-index($string, $search);

  @if $index {
    @return str-slice($string, 1, $index - 1) + $replace + str-replace(str-slice($string, $index + str-length($search)), $search, $replace);
  }

  @return $string;

}

@mixin pseudo-clickable-block {
  &::after {
    @include z-index;
    content: '';
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
  }
}

// Border-radius - returns a border-radius from $border-radii map
@mixin border-radius($type: default) {
  @if $type != 'none' and $type != 'default' and $type != 'round' and $type != 'tag' {
    @error 'NSW-DS get-border-radius mixin allows the following values for $type: none, default, round, tag. You have used "#{$type}"';
  }

  @if $type == 'round' {
    border-radius: map-get($border-radii, $type);
  } @else {
    border-radius: rem(map-get($border-radii, $type));
  }
}

// Z-index
@mixin z-index($index: 'base') {

  @if map-has-key($z-index, $index) == false {
    @error 'NSW-DS z-index mixin allows the following values for $index: #{map-keys($z-index)}. You have used "#{$index}"';
  }
  @else {
    z-index: map-get($z-index, $index);
  }
}

@mixin border($direction: null, $width: sm, $color: $nsw-primary-blue) {
  @if map-has-key($border-width, $width) == false {
    @error 'NSW-DS border mixin allows the following values for $width: #{map-keys($border-width)}. You have used "#{$width}"';
  }
  @if $direction != null and $direction != 'left' and $direction != 'top' and $direction != 'bottom' and $direction != 'right' {
    @error 'NSW-DS border mixin allows the following values for $direction: left, top, bottom, right. You have used "#{$direction}"';
  }

  $properties: if($width == 'none', map-get($border-width, $width), solid map-get($border-width, $width) $color);

  @if $direction == null {
    border: $properties;
  }
  @else {
    border-#{$direction}: $properties;
  }
}
