/**
 * @license
 * Copyright Akveo. All Rights Reserved.
 * Licensed under the MIT License. See License.txt in the project root for license information.
 */


/**
 * This is a starting point where we declare the maps of themes and globally available functions/mixins
 */

@import 'core/mixins';
@import 'core/functions';

$nga-themes: () !global;
$nga-themes-non-processed: () !global;
$nga-enabled-themes: () !global;


@function nga-theme($key) {
  @return map-get($theme, $key);
}

@function nga-get-value($theme, $key, $value) {
  @if (type-of($value) == 'string') {
    $tmp: map-get($theme, $value);

    @if ($tmp != null) {
      @return nga-get-value($theme, $value, $tmp);
    }
  }

  @return map-get($theme, $key);
}

@function nga-register-theme($theme, $name, $default: null) {


  @if ($default != null) {

    $theme: map-merge(map-get($nga-themes-non-processed, $default), $theme);
    $nga-themes-non-processed: map-set($nga-themes-non-processed, $name, $theme) !global;
  } @else {
    $nga-themes-non-processed: map-set($nga-themes-non-processed, $name, $theme) !global;
  }

  $theme-parsed: ();
  @each $key, $value in $theme {
    $theme-parsed: map-set($theme-parsed, $key, nga-get-value($theme, $key, $value));
  }

  @return map-set($nga-themes, $name, $theme-parsed);
}

@function get-enabled-themes() {
  $themes-to-install: ();

  @if (length($nga-enabled-themes) > 0) {
    @each $theme-name in $nga-enabled-themes {
      $themes-to-install: map-set($themes-to-install, $theme-name, map-get($nga-themes, $theme-name));
    }
  } @else {
    $themes-to-install: $nga-themes;
  }

  @return $themes-to-install;
}
// TODO: we hide :host inside of it which is not obvious
@mixin nga-install-component() {

  $themes-to-install: get-enabled-themes();

  @each $theme-name, $theme in $themes-to-install {
    :host-context(.nga-theme-#{$theme-name}) {
      $theme: $theme !global;
      @content;
    }
  }
}

// TODO: another mixing for the almost same thing
@mixin nga-install-root-component() {

  $themes-to-install: get-enabled-themes();

  @each $theme-name, $theme in $themes-to-install {
    :host .nga-theme-#{$theme-name} {
      $theme: $theme !global;
      @content;
    }
  }
}

@mixin nga-install-global() {
  $themes-to-install: get-enabled-themes();

  @each $theme-name, $theme in $themes-to-install {
    .nga-theme-#{$theme-name} {
      $theme: $theme !global;

      @content;
    }
  }
}
