// PatternFly functions
// --------------------------------------------------
@import "./div";

// Transform px to rems
@function pf-strip-unit($num) {
  @return div($num, ($num * 0 + 1));
}

@function pf-size-prem($pxval, $base: $pf-global--space-size-root) {
  @return div(pf-strip-unit($pxval), $base) * 1rem;
}

@function pf-font-prem($pxval, $base: $pf-global--font-size-root) {
  @return div(pf-strip-unit($pxval), $base) * 1rem;
}

@function pf-bg-svg($svg-coordinates, $viewBox: "512", $svg-color: "currentColor") {
  $color: str-slice(#{$svg-color}, 2);

  @return url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 #{$viewBox} 512'%3E%3Cpath fill='%23#{$color}' d='#{$svg-coordinates}'/%3E%3C/svg%3E");
}

// Return (width) breakpoint value if it exists
@function pf-breakpoint-value($breakpoint, $breakpoint-map: $pf-global--breakpoint-name-map) {
  $breakpoint-value: if(map-has-key($breakpoint-map, #{$breakpoint}), map-get($breakpoint-map, #{$breakpoint}), false);

  @return #{$breakpoint-value};
}

// Return height breakpoint value if it exists
@function pf-height-breakpoint-value($height-breakpoint, $height-breakpoint-map: $pf-global--height-breakpoint-name-map) {
  $height-breakpoint-value: if(map-has-key($height-breakpoint-map, #{$height-breakpoint}), map-get($height-breakpoint-map, #{$height-breakpoint}), false);

  @return #{$height-breakpoint-value};
}

// Build breakpoint map
// Based on $pf-global--breakpoint-name-map
@function build-breakpoint-map($map-items...) {
  $map: ();

  @if length($map-items) == 0 {
    $map: ("base": null);
    $map: map-merge($map, $pf-global--breakpoint-name-map);
  }

  @else {
    @each $breakpoint in $map-items {
      @if not map-has-key($pf-global--breakpoint-name-map, $breakpoint) and $breakpoint != "base" {
        $map: map-merge($map, ("invalid breakpoint #{$breakpoint}": null));
      }

      @else if $breakpoint == "base" {
        $map: map-merge($map, ($breakpoint: null));
      }

      @else {
        $map: map-merge($map, ($breakpoint: map-get($pf-global--breakpoint-name-map, #{$breakpoint})));
      }
    }
  }

  @return $map;
}

// Build height breakpoint map
// Based on $pf-global--height-breakpoint-name-map
@function build-height-breakpoint-map($map-items...) {
  $map: ();

  @if length($map-items) == 0 {
    $map: ("base": null);
    $map: map-merge($map, $pf-global--height-breakpoint-name-map);
  }

  @else {
    @each $breakpoint in $map-items {
      @if not map-has-key($pf-global--height-breakpoint-name-map, $breakpoint) and $breakpoint != "base" {
        $map: map-merge($map, ("invalid breakpoint #{$breakpoint}": null));
      }

      @else if $breakpoint == "base" {
        $map: map-merge($map, ($breakpoint: null));
      }

      @else {
        $map: map-merge($map, ($breakpoint: map-get($pf-global--height-breakpoint-name-map, #{$breakpoint})));
      }
    }
  }

  @return $map;
}

// Build spacer map
// Based on $pf-global--spacer-map
@function build-spacer-map($map-items...) {
  $map: ();

  @if length($map-items) == 0 {
    $map: ("none": 0);
    $map: map-merge($map, $pf-global--spacer-map);
    $map: map-remove($map, "auto", "0");
  }

  @each $spacer in $map-items {
    @if not map-has-key($pf-global--spacer-map, $spacer) and $spacer != "none" {
      $map: map-merge($map, ("invalid spacer #{$spacer}": null));
    }

    @else if $spacer == "none" {
      $map: map-merge($map, ($spacer: 0));
    }

    @else {
      $map: map-merge($map, ($spacer: map-get($pf-global--spacer-map, #{$spacer})));
    }
  }

  @return $map;
}

// Build variable map
// Based on custom map
@function build-variable-map($namespace, $map: ()) {
  $new-map: ();

  @each $size, $value in $map {
    $new-map: map-merge($new-map, (map-get($map, $size): --#{$namespace}--#{$size}));
  }

  @return $new-map;
}
