/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/

// Generates map from array.
// Key of each entry is ($array entry value: result of $fn call).
// $fn is called with specified $args.
// Usage example:
/*
  $counter: 0;
  @function step($step) {
    $counter: $counter + $step !global;
    @return $counter;
  }
  uicore-map-from-array($array, step, 1000);
*/
@function uicore-map-from-array($array, $fn, $args...) {
  $map: ();
  @each $item in $array {
    $kvp: (
      $item: call($fn, $args...)
    );
    $map: map-merge($map, $kvp)
  }
  @return $map;
}

// Generates map from array.
// Key of each entry is ($array entry value: result of $fn call).
// $fn is called with one argument:
// 1. First entry with $initialArg
// 2. Other entries with previous result of $fn call.
// Usage example:
/*
  @function step($counter) {
    @return $counter + 1000;
  }
  uicore-map-from-array-with-result-arg($array, step, 0);
*/
@function uicore-map-from-array-with-result-arg($array, $fn, $initialArg) {
  $map: ();
  $result: $initialArg;
  @each $item in $array {
    $result: call($fn, $result);
    $kvp: ($item: $result);
    $map: map-merge($map, $kvp)
  }
  @return $map;
}

