// Copyright 2017 Palantir Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0.

// these implementations borrowed from Bourbon v5, and modified to fit our use cases.
// changes: removed $asset-pipeline argument, added weight/style params like Bourbon v4.
// https://github.com/thoughtbot/bourbon/blob/master/core/bourbon/library/_font-face.scss
// https://github.com/thoughtbot/bourbon/blob/master/core/bourbon/utilities/_font-source-declaration.scss

// Generates an `@font-face` declaration. You can choose the specific file
// formats you need to output; the mixin supports `eot`, `ttf`, `svg`, `woff2`
// and `woff`.
//
// @argument {string} $font-family
//
// @argument {string} $file-path
//
// @argument {string | list} $file-formats
//   List of the font file formats to include.
//
// @argument {string} $weight
//
// @argument {string} $style
//
// @content
//   Any additional CSS properties that are included in the `@include`
//   directive will be output within the `@font-face` declaration, e.g. you can
//   pass in `font-weight`, `font-style` and/or `unicode-range`.
@mixin font-face(
  $font-family,
  $file-path,
  $file-formats,
  $font-weight: normal,
  $font-style: normal
) {
  @font-face {
    font-family: $font-family;
    font-style: $font-style;
    font-weight: $font-weight;

    src: font-source-declaration(
      $font-family,
      $file-path,
      $file-formats
    );

    @content;
  }
}

@function font-source-declaration($font-family, $file-path, $file-formats) {
  $src: ();

  $formats-map: (
    eot:   "#{$file-path}.eot?#iefix" format("embedded-opentype"),
    woff2: "#{$file-path}.woff2" format("woff2"),
    woff:  "#{$file-path}.woff" format("woff"),
    ttf:   "#{$file-path}.ttf" format("truetype"),
    svg:   "#{$file-path}.svg##{$font-family}" format("svg"),
  );

  @each $key, $values in $formats-map {
    @if list-contains($file-formats, $key) {
      $file-path: nth($values, 1);
      $font-format: nth($values, 2);

      $src: append($src, url($file-path) $font-format, comma);
    }
  }

  @return $src;
}

// Returns true if `$list` contains any of `$values`.
@function list-contains($list, $values...) {
  @each $value in $values {
    @if type-of(index($list, $value)) != "number" {
      @return false;
    }
  }

  @return true;
}
