@charset 'UTF-8';

////
/// Flavor SCSS Types Array
/// @group types-array
/// @author blackmirror1980
////

/// Checks if a value is an array/list
///
/// @access public
/// @param {list | array} $a - the value to check
/// @return {boolean} - true if is an array value
@function is-array($a) {
  @return is-defined($a) and type-of($a)==list;
}

@function is-array-comma($a) {
  @return is-array($a) and array-separator($a)==comma;
}

@function is-array-space($a) {
  @return is-array($a) and array-separator($a)==space;
}

/// Checks if an array/list contains specified item
///
/// @access public
/// @param {list | array} $a - the array/list
/// @param {any} $i - the item
/// @requires {function} is-array
/// @requires {function} is-defined
/// @return {boolean} - true if is the array contains the value
@function array-contains($a, $i) {
  @if is-array($a) and is-defined($i) {
    $index-of: index($a, $i);

    @return is-defined($index-of) and $index-of > 0;
  }

  @warn '`array: #{$a}` and `item: #{$i}` are not valid data for array-contains';
  @return false;
}

/// Concats two or more arrays/lists
///
/// @access public
/// @param {list | array} $lists... - the arrays/lists
/// @return {list | array} - the concatenated array/list
@function array-concat($lists...) {
  $concat-list: ();

  @each $list in $lists {
    @if is-array($list) {
      @each $item in $list {
        $concat-list: append($concat-list, $item);
      }
    }
    @else {
      $concat-list: append($concat-list, $list);
    }
  }

  @return $concat-list;
}

/// Joins an array/list in a string with the specified separator
///
/// @access public
/// @param {list | array} $a - the array/list
/// @param {string} $sep [','] - the sparator
/// @return {string} - the joined string
@function array-join($a, $sep: ', ') {
  $join-string: '';

  @each $item in $a {
    $join-string: $join-string + $sep + $item;
  }

  $join-string: str-slice($join-string, str-length($sep) + 1);

  @return $join-string;
}

@function array-remove($list, $value) {
  $result: ();

  @each $item in $list {
    @if $item != $value {
      $result: append($result, $item, list-separator($list));
    }
  }

  @return $result;
}

@function array-remove-nth($list, $index) {
  $result: ();

  @each $item in $list {
    $index-of: index($list, $item);

    @if $index-of != $index {
      $result: append($result, $item, list-separator($list));
    }
  }

  @return $result;
}

@function array-head($list) {
  @return nth-value($list, 1);
}

@function array-tail($list) {
  @return array-remove-nth($list, 1);
}

@function array-separator($list) {
  @return list-separator($list);
}

@function array-reverse($list) {
  @if is-array($list) {
    $list-reverse: ();

    @for $i from (length($list) * -1) through -1 {
      $list-reverse: append($list-reverse, nth($list, abs($i)), list-separator($list));
    }

    @return $list-reverse;
  }

  @return $list;
}
