/**
 * 方法
 * @Author 瞿龙俊 - qulongjun@shine.design
 * @Date 2020-03-22 13:01
 */
@import 'config';

@function shine-unitless($number) {
  @if type-of($number) == 'number' and not unitless($number) {
    @return $number / ($number * 0 + 1);
  }

  @return $number;
}

@function shine-get($map, $keys...) {
  @if length($keys) == 1 {
    $keys: nth($keys, 1);
  }

  $warn: "#{nth($keys, 1)}";
  $length: length($keys);
  $get: map-get($map, nth($keys, 1));

  @if $length > 1 {
    @for $i from 2 through $length {
      @if $get != null and type-of($get) == 'map' {
        $warn: $warn + "->#{nth($keys, $i)}";
        $get: map-get($get, nth($keys, $i));

        @if $get == null {
          @return null;
        }
      }
      @else {
        @return shine-get-warning($warn, $get, nth($keys, $i));
      }
    }
  }

  @return $get;
}

@function shine-has($map, $keys...) {
  @if length($keys) == 1 {
    $keys: nth($keys, 1);
  }

  $warn: "#{nth($keys, 1)}";
  $length: length($keys);
  $get: map-get($map, nth($keys, 1));

  @if $length > 1 {
    @for $i from 2 through $length {
      @if $get != null and type-of($get) == 'map' {
        $warn: $warn + "->#{nth($keys, $i)}";
        $get: map-get($get, nth($keys, $i));

        @if $get == null {
          @return false;
        }
      }
      @else {
        @return false;
      }
    }
  }

  @if $get != null {
    @return true;
  }
  @else {
    @return false;
  }
}

@function shine-get-warning($warn, $get, $key) {
  @if $get == null {
    @warn "Map has no value for key search `#{$warn}`";
  }
  @else if type-of($get) != 'map' {
    @warn "Non-map value found for key search `#{$warn}`, cannot search for key `#{$key}`";
  }
  @return null;
}

@function shine-display-color($color) {
  @if (lightness( $color ) > 50) {
    @return darken($color, 50%);
  }
  @else {
    @return lighten($color, 50%);
  }
}

@function shine-state-color($name, $type: null) {
  @if ($type == null) {
    $type: base;
  }

  @return shine-get($shine-state-colors, $name, $type);
}

@function shine-brand-color($type: null) {
  @return shine-state-color(brand, $type);
}

@function shine-base-color($type, $level) {
  @return shine-get($shine-base-colors, $type, $level);
}

@function shine-media-breakpoint($type) {
  @if (kt-has($shine-media-breakpoints, $type)) {
    @return shine-get($shine-media-breakpoints, $type);
  } @else {
    @return $type;
  }
}
