// Copyright 2018-Present Okta, Inc.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

@function ms($step) {
  $value: 1rem;

  @if $step > 0 {
    @for $i from 1 through $step {
      $value: ($value * $scale-ratio);
    }
  }

  @if $step < 0 {
    @for $i from $step through -1 {
      $value: ($value / $scale-ratio);
    }
  }

  @return $value;
}

// TODO: get rid of this
@function rem-to-px($value) {
  @if variable-exists('base-font-size') {
    @return ($value / 1rem) * $base-font-size;
  }
}

/**
 * Simple string replacement
 */

@function str-replace($string, $search, $replace: '') {
  $index: str-index($string, $search);

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

/**
 * Retrieves map keys from nested maps.
 */

@function map-deep-get($map, $keys...) {
  @each $key in $keys {
    $map: map-get($map, $key);
  }

  @return $map;
}

/**
 * Retrieves and encodes icons for use in CSS
 * Works down to IE9
 *
 * Refs:
 * https://css-tricks.com/creating-a-maintainable-icon-system-with-sass/
 * https://codepen.io/chriscoyier/pen/ZQgvyG/
 * https://codepen.io/tigt/post/optimizing-svgs-in-data-uris
 * https://codepen.io/jakob-e/pen/doMoML
 */

$icons: (
  'arrow':'<svg viewBox="0 0 10 12" fill="none" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M4.29289 0.292893C4.68342 -0.0976311 5.31658 -0.0976311 5.70711 0.292893L9.70711 4.29289C10.0976 4.68342 10.0976 5.31658 9.70711 5.70711C9.31658 6.09763 8.68342 6.09763 8.29289 5.70711L6 3.41421V11C6 11.5523 5.55228 12 5 12C4.44772 12 4 11.5523 4 11V3.41421L1.70711 5.70711C1.31658 6.09763 0.683417 6.09763 0.292893 5.70711C-0.0976311 5.31658 -0.0976311 4.68342 0.292893 4.29289L4.29289 0.292893Z" fill="%%COLOR%%"/></svg>',
  'anchor': '<svg fill="none" viewBox="0 0 24 24"><defs/><path fill="%%COLOR%%" d="M14.264 9.733a4.8 4.8 0 00-6.787 0l-4.36 4.363a4.8 4.8 0 003.393 8.19 4.768 4.768 0 003.39-1.399l3.6-3.6a.343.343 0 00-.243-.585h-.137a5.769 5.769 0 01-2.194-.426.343.343 0 00-.374.076l-2.588 2.591a2.058 2.058 0 11-2.91-2.91L9.43 11.66a2.057 2.057 0 012.907 0c.541.51 1.385.51 1.926 0 .233-.233.374-.542.398-.87a1.371 1.371 0 00-.398-1.056z"/><path fill="%%COLOR%%" d="M20.877 3.12a4.8 4.8 0 00-6.787 0l-3.596 3.592a.343.343 0 00.25.587h.127a5.759 5.759 0 012.19.428.343.343 0 00.374-.075l2.581-2.578a2.058 2.058 0 112.91 2.91l-3.215 3.212-.027.031-1.125 1.118a2.057 2.057 0 01-2.907 0 1.406 1.406 0 00-1.926 0 1.371 1.371 0 00-.398.877c-.025.393.12.777.398 1.056a4.76 4.76 0 001.37.96c.073.034.145.062.217.092.072.031.147.055.22.083.071.027.147.051.219.072l.202.054c.137.035.274.062.415.086.169.025.34.041.51.048h.26l.207-.024c.075-.003.154-.02.243-.02h.117l.236-.035.11-.02.199-.041h.037a4.799 4.799 0 002.225-1.262l4.364-4.364a4.8 4.8 0 000-6.787z"/></svg>',
  'caret':'<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 16 16"><path fill="%%COLOR%%" fill-rule="evenodd" d="M8.73105 9.21673c-.39533.42357-1.06677.42357-1.46211 0L3.13105 4.78327c-.39533-.42357-1.06677-.42357-1.46211 0l-.03211.03441c-.35859.38421-.35859.98043 0 1.36464l5.62704 6.02898c.3972.4256 1.07254.4232 1.4668-.0051l5.62423-6.11001c.3604-.39146.3473-.99759-.0295-1.37318-.3967-.39541-1.042-.38376-1.4242.02572l-4.17015 4.468z" clip-rule="evenodd"/></svg>',
  'check':'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 12 12"><path fill="%%COLOR%%" d="M0 5.6L1.6 4l3.2 3.3 5.6-5.7L12 3.1l-7.2 7.4L0 5.6z"/></svg>',
  'close':'<svg viewBox="0 0 14 14" fill="none" xmlns="http://www.w3.org/2000/svg" class="ods-icon"><path d="M2.32289 10.1181C1.89237 10.5486 1.89237 11.2466 2.32289 11.6771C2.7534 12.1076 3.45141 12.1076 3.88192 11.6771L7 8.55904L10.1181 11.6771C10.5486 12.1076 11.2466 12.1076 11.6771 11.6771C12.1076 11.2466 12.1076 10.5486 11.6771 10.1181L8.55904 7L11.6771 3.88193C12.1076 3.45141 12.1076 2.7534 11.6771 2.32289C11.2466 1.89237 10.5486 1.89237 10.1181 2.32289L7 5.44096L3.88193 2.32289C3.45141 1.89237 2.7534 1.89237 2.32289 2.32289C1.89237 2.7534 1.89237 3.45141 2.32289 3.88192L5.44096 7L2.32289 10.1181Z" fill="%%COLOR%%"/></svg>',
  'dash':'<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 10 2"><path fill="%%COLOR%%" d="M0 0h10v2H0z"/></svg>',
  'double-arrow':'<svg viewBox="0 0 12 12" fill="none" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M2.29289 0.292893C2.68342 -0.0976311 3.31658 -0.0976311 3.70711 0.292893L5.70711 2.29289C6.09763 2.68342 6.09763 3.31658 5.70711 3.70711C5.31658 4.09763 4.68342 4.09763 4.29289 3.70711L4 3.41421V10C4 10.5523 3.55228 11 3 11C2.44772 11 2 10.5523 2 10V3.41421L1.70711 3.70711C1.31658 4.09763 0.683417 4.09763 0.292893 3.70711C-0.0976311 3.31658 -0.0976311 2.68342 0.292893 2.29289L2.29289 0.292893Z" fill="%%COLOR%%"/><path fill-rule="evenodd" clip-rule="evenodd" d="M9 12C8.73478 12 8.48043 11.8946 8.29289 11.7071L6.29289 9.70711C5.90237 9.31658 5.90237 8.68342 6.29289 8.29289C6.68342 7.90237 7.31658 7.90237 7.70711 8.29289L8 8.58579L8 2C8 1.44771 8.44772 1 9 1C9.55229 1 10 1.44771 10 2L10 8.58579L10.2929 8.29289C10.6834 7.90237 11.3166 7.90237 11.7071 8.29289C12.0976 8.68342 12.0976 9.31658 11.7071 9.70711L9.70711 11.7071C9.51957 11.8946 9.26522 12 9 12Z" fill="%%COLOR%%"/></svg>',
  'external':'<svg fill="none" viewBox="0 0 14 14"><defs/><path fill="%%COLOR%%" fill-rule="evenodd" d="M1.375 4.75c0-.76.616-1.375 1.375-1.375H5a.625.625 0 110 1.25H2.75a.125.125 0 00-.125.125v6.5c0 .069.056.125.125.125h6.5a.125.125 0 00.125-.125V9a.625.625 0 111.25 0v2.25c0 .76-.616 1.375-1.375 1.375h-6.5c-.76 0-1.375-.616-1.375-1.375v-6.5z" clip-rule="evenodd"/><path fill="%%COLOR%%" fill-rule="evenodd" d="M7.375 2c0-.345.28-.625.625-.625h4c.345 0 .625.28.625.625v4a.625.625 0 11-1.25 0V3.509L5.442 9.442a.625.625 0 11-.884-.884l5.933-5.933H8A.625.625 0 017.375 2z" clip-rule="evenodd"/></svg>',
  'search':'<svg viewBox="0 0 14 14" fill="none" xmlns="http://www.w3.org/2000/svg" class="ods-icon"><path fill-rule="evenodd" clip-rule="evenodd" d="M8.75739 10.1716C7.96696 10.6951 7.01908 11 6 11C3.23858 11 1 8.76142 1 6C1 3.23858 3.23858 1 6 1C8.76142 1 11 3.23858 11 6C11 7.01908 10.6951 7.96696 10.1716 8.75739L12.6908 11.2766C13.0813 11.6671 13.0813 12.3003 12.6908 12.6908C12.3003 13.0813 11.6671 13.0813 11.2766 12.6908L8.75739 10.1716ZM9 6C9 7.65685 7.65685 9 6 9C4.34315 9 3 7.65685 3 6C3 4.34315 4.34315 3 6 3C7.65685 3 9 4.34315 9 6Z" fill="%%COLOR%%"/></svg>',
);

@function get-icon($icon, $color) {
  // Check for valid color
  @if 'color' != type-of( $color ) {
    @warn 'The requested color - "' + $color + '" - was not recognized as a Sass color value.';

    @return null;
  }

  @if map-has-key( $icons, $icon ) {
    $icon: map-get($icons, $icon);
    $color-placeholder: '%%COLOR%%';
    $icon: str-replace($icon, $color-placeholder, $color);
  } @else {
    @warn 'The requested icon - "' + $icon + '" - is not defined in the $icons map.';

    @return null;
  }

  // Add missing namespace
  @if not str-index($icon,xmlns) {
    $icon: str-replace($icon, '<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($icon)/$slice);

  @for $i from 1 through $loops {
    $chunk: str-slice($icon, $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');

    $encoded: #{$encoded}#{$chunk};
    $index: $index + $slice;
  }

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