/* ==========================================================================
   ICSS-JS EXPORTS
 * ========================================================================== */

@use 'sass:math';
@use 'sass:meta';
@use 'sass:string';

/**
 * Export SASS data structures to JSON-encoded value we can parse from JS
 */
@function icss-export($value) {
  @return json-encode($value);
}

/**
 * JSON-encode an arbitrary variable
 */
@function json-encode($value) {
  $type: meta.type-of($value);
  @if meta.function-exists('json-encode-#{$type}') {
    @return meta.call(meta.get-function('json-encode-#{$type}'), $value);
  }
  @error 'Unknown type for #{$value} (#{$type}).';
}

/**
 * JSON-encode a map
 */
@function json-encode-map($map) {
  $str: '';
  @each $key, $value in $map {
    $str: $str + ', ' + json-quote($key) + ':' + json-encode($value);
  }
  @return '{' + string.slice($str, 3) + '}';
}

/**
 * JSON-encode a list
 */
@function json-encode-list($list) {
  $str: '';
  @each $item in $list {
    $str: $str + ', ' + json-encode($item);
  }
  @return '[' + string.slice($str, 3) + ']';
}

/**
 * JSON-encode a string
 */
@function json-encode-string($string) {
  @return json-quote($string);
}

/**
 * JSON-encode a number
 */
@function json-encode-number($number) {
  @return if(math.is-unitless($number), $number, json-quote($number));
}

/**
 * JSON-encode a color
 */
@function json-encode-color($color) {
  @return json-quote($color);
}

/**
 * JSON-encode a boolean
 */
@function json-encode-bool($boolean) {
  @return $boolean;
}

/**
 * Quote a JSON key or value
 */
@function json-quote($value) {
  @return '"#{$value}"';
}
