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

@use 'sass:map';
@use 'sass:meta';
@use '../../theming-variables';
@use 'get-value';
@use 'register';

@mixin nb-for-theme($name) {
  @if (theming-variables.$nb-theme-name == $name) {
    @content;
  }
}

@mixin nb-for-themes($names...) {
  @each $name in $names {
    @include nb-for-theme($name) {
      @content;
    }
  }
}

@mixin nb-except-theme($name) {
  @if (theming-variables.$nb-theme-name != $name) {
    @content;
  }
}

@mixin nb-except-for-themes($names...) {
  @each $name in $names {
    @include nb-except-theme($name) {
      @content;
    }
  }
}

@mixin nb-install-css-properties($theme-name, $theme) {
  .nb-theme-#{$theme-name} {
    @each $var, $value in $theme {
      @if (meta.type-of($value) == 'string' and map.get($theme, $value)) {
        --#{$var}: var(--#{$value});
      } @else {
        --#{$var}: #{$value};
      }
    }
  }
}

@mixin nb-pre-process-context($theme-name) {
  theming-variables.$nb-theme-process-mode: 'pre-process';

  theming-variables.$nb-theme-name: $theme-name;
  theming-variables.$nb-processed-theme: get-value.nb-process-theme(register.nb-get-registered-theme($theme-name));
}

@mixin nb-lazy-process-context($theme-name) {
  theming-variables.$nb-theme-process-mode: 'lazy-process';

  theming-variables.$nb-theme-name: $theme-name;
  theming-variables.$nb-processed-theme: ();
}

@mixin nb-install-component-with-css-props() {
  // @breaking-change 5.0.0
  :host {
    @content;
  }
}

@mixin nb-install-component-with-scss-vars() {
  $enabled-themes: register.nb-get-enabled-themes();

  @each $theme-name in $enabled-themes {
    @include nb-lazy-process-context($theme-name);

    /*
      :host can be prefixed
      https://github.com/angular/angular/blob/8d0ee34939f14c07876d222c25b405ed458a34d3/packages/compiler/src/shadow_css.ts#L441

      We have to use :host instead of :host-context($theme), to be able to prefix theme class
      with something defined inside of @content, by prefixing &.
      For example this scss code:
        .nb-theme-default {
          .some-selector & {
            ...
          }
        }
      Will result in next css:
        .some-selector .nb-theme-default {
          ...
        }

      It doesn't work with :host-context because angular splitting it in two selectors and removes
      prefix in one of the selectors.
    */
    .nb-theme-#{$theme-name} :host {
      @content;
    }
  }
}

// Entry point
// Installs component styles based on registered themes
// TODO: we hide :host inside of it which is not obvious
@mixin nb-install-component() {
  @if (theming-variables.$nb-enable-css-custom-properties) {
    @include nb-install-component-with-css-props() {
      @content;
    }
  } @else {
    @include nb-install-component-with-scss-vars() {
      @content;
    }
  }
}

@mixin nb-install-global-with-css-props() {
  @content;

  @if (theming-variables.$nb-global-styles-on-install) {
    @each $theme-name in register.nb-get-enabled-themes() {
      @include nb-install-css-properties($theme-name, register.nb-get-registered-theme($theme-name));
    }
  }
}

@mixin nb-install-global-with-scss-vars() {
  @each $theme-name in register.nb-get-enabled-themes() {
    @include nb-pre-process-context($theme-name);

    .nb-theme-#{$theme-name} {
      @content;
    }
  }
}

// Entry point
// Installs global styles based on registered themes
@mixin nb-install() {
  @if (theming-variables.$nb-enable-css-custom-properties) {
    @include nb-install-global-with-css-props() {
      @content;
    }
  } @else {
    @include nb-install-global-with-scss-vars() {
      @content;
    }
  }
}
