@import '../../themes/mapping';

/**
 * @license
 * Copyright Endlessjs. All Rights Reserved.
 * Licensed under the MIT License. See License.txt in the project root for license information.
 */

@function el-get-enabled-themes() {

  @if (length($el-enabled-themes) == 0) {
    @each $theme-name, $theme in $el-themes {
      $el-enabled-themes: append($el-enabled-themes, $theme-name) !global;
    }
  }
  @return $el-enabled-themes;
}

@function get-last-enabled-theme() {
  $themes: el-get-enabled-themes();
  @return nth($themes, length($themes));
}

@function el-set-for-export($theme, $name, $parent-name: null) {

  $parent-theme: map-get($el-themes-export, $parent-name);
  @if ($parent-theme != null) {
    $theme: map-merge(map-get($parent-theme, data), $theme);
  }

  $theme-data: (
    data: $theme,
    parent: $parent-name,
  );
  @return map-set($el-themes-export, $name, $theme-data);
}

@function el-get-registered-theme($name) {
  $theme: map-get($el-themes, $name);

  // TODO: check if optimal place
  @if ($theme == null) {
    @error 'Endlessjs Theme: theme `' + $name + '` is not registered with `el-register-theme` function.';
  }

  @return $theme;
}

// Entry point
// Registers a new theme
@function el-register-theme($theme, $name, $parent-name: null) {

  @if ($el-theme-export-mode == true) {
    $el-themes-export: el-set-for-export($theme, $name, $parent-name) !global;
  }

  $theme-data: ();

  @if ($parent-name != null) {
    $parent-theme: map-get($el-themes, $parent-name);
    @if ($parent-theme == null) {
      @error 'Endlessjs Theme: parent theme `' + $parent-name + '` is not registered or imported.';
    }
    $theme: map-merge($parent-theme, $theme);
  }
  $theme: map-merge($eva-mapping, $theme);
  $el-themes: map-set($el-themes, $name, $theme) !global;

  @return $el-themes;
}
