@use 'sass:meta';
@use './index' as theme;

@function theme-value($var: '', $debug: true) {
  $index: str-index($var, '-');
  $key: str-slice($var, $index + 1);
  $module-name: str-slice($var, 0, $index - 1);

  @if meta.global-variable-exists($var, theme) {
    @return map-get(meta.module-variables(theme), $var);
  }

  @if meta.global-variable-exists($key, theme) {
    @return map-get(meta.module-variables(theme), $key);
  }

  @if $debug {
    @error '@datago/theme: var '#{$var}' is not defined!';
  }

  @return false;
}

@function theme-var($var, $default: null, $show-value: false) {
  $value: theme-value($var, false);
  @if $value == false {
    @if $default == null {
      @error "@datago/theme: Css property var '--#{$var}' is not defined, default value is required!";
    }

    @warn "@datago/theme: Css property var '--#{$var}' is not defined, default value '#{$default}' will be settled";

    @return var(--#{$var}, $default);
  }

  @if $show-value {
    @return var(--#{$var}, $value);
  }

  @return var(--#{$var});
}

@function theme-var-value($var) {
  @return theme-var($var, null, true);
}
