/*---------------------------------------------------------------------------------------------
 * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
 * See LICENSE.md in the project root for license terms and full copyright notice.
 *--------------------------------------------------------------------------------------------*/
@use "sass:map";
@use "sass:meta";

// 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: meta.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: meta.call($fn, $result);
    $kvp: (
      $item: $result,
    );
    $map: map.merge($map, $kvp);
  }
  @return $map;
}
