//
// Copyright 2018 Google Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

// stylelint-disable selector-class-pattern --
// Selector '.mdc-*' should only be used in this project.

@use 'sass:math';
@use '../elevation/mixins' as elevation-mixins;
@use '../feature-targeting/feature-targeting';
@use '../ripple/ripple-theme';
@use '../rtl/rtl';
@use '../list/mixins' as list-mixins;
@use '../list/variables' as list-variables;
@use '../ripple/ripple';
@use '../theme/theme';
@use '../theme/custom-properties';
@use './menu-custom-properties';
@use './variables';

@mixin core-styles($query: feature-targeting.all()) {
  $feat-color: feature-targeting.create-target($query, color);
  $feat-structure: feature-targeting.create-target($query, structure);

  @include ripple.common($query); // COPYBARA_COMMENT_THIS_LINE
  // prettier-ignore
  @include elevation-mixins.overlay-common($query); // COPYBARA_COMMENT_THIS_LINE

  // Customize the menu-surface and contained list to match the mdc-menu styles.
  .mdc-menu {
    @include list-mixins.deprecated-item-meta-ink-color(
      variables.$ink-color,
      $query
    );
    @include list-mixins.deprecated-item-graphic-ink-color(
      variables.$ink-color,
      $query
    );
    @include min-width(variables.$min-width, $query: $query);
    @include open-item-opacity(variables.$open-item-opacity, $query: $query);
    // Include mdc-deprecated-list selector to ensure list-inside-menu overrides default list styles
    .mdc-deprecated-list {
      @include list-mixins.deprecated-item-primary-text-ink-color(
        variables.$ink-color,
        $query
      );
    }

    .mdc-deprecated-list,
    .mdc-list {
      @include elevation-mixins.overlay-surface-position($query: $query);
      @include elevation-mixins.overlay-dimensions(100%, $query: $query);
    }

    .mdc-deprecated-list-divider {
      @include feature-targeting.targets($feat-structure) {
        margin: 8px 0;
      }
    }

    .mdc-deprecated-list-item {
      @include feature-targeting.targets($feat-structure) {
        user-select: none;
      }
    }

    .mdc-deprecated-list-item--disabled {
      @include feature-targeting.targets($feat-structure) {
        cursor: auto;
      }
    }

    //stylelint-disable selector-no-qualifying-type
    a.mdc-deprecated-list-item .mdc-deprecated-list-item__text,
    a.mdc-deprecated-list-item .mdc-deprecated-list-item__graphic {
      @include feature-targeting.targets($feat-structure) {
        pointer-events: none;
      }
    }
    // stylelint-enable selector-no-qualifying-type
  }

  // postcss-bem-linter: define menu
  .mdc-menu__selection-group {
    @include feature-targeting.targets($feat-structure) {
      padding: 0;
      fill: currentColor;
    }

    .mdc-deprecated-list-item {
      @include feature-targeting.targets($feat-structure) {
        @include rtl.reflexive-property(
          padding,
          56px,
          list-variables.$deprecated-side-padding
        );
      }
    }

    // Extra specificity required to override `display` property on `mdc-deprecated-list-item__graphic`.
    .mdc-menu__selection-group-icon {
      @include feature-targeting.targets($feat-structure) {
        @include rtl.reflexive-position(left, 16px);

        display: none;
        position: absolute;
        // IE11 requires the icon to be vertically centered due to its absolute positioning
        top: 50%;
        transform: translateY(-50%);
      }
    }
  }
  // postcss-bem-linter: end

  // stylelint-disable-next-line plugin/selector-bem-pattern
  .mdc-menu-item--selected .mdc-menu__selection-group-icon {
    @include feature-targeting.targets($feat-structure) {
      display: inline;
    }
  }
}

@mixin open-item-opacity($opacity, $query: feature-targeting.all()) {
  $feat-color: feature-targeting.create-target($query, color);

  .mdc-menu-item--submenu-open {
    @include ripple-theme.states-background-selector(
      list-variables.$deprecated-ripple-target
    ) {
      @include feature-targeting.targets($feat-color) {
        opacity: $opacity;
      }
    }
    @include ripple-theme.states-background-selector(
      list-variables.$deprecated-temporary-ripple-target
    ) {
      @include feature-targeting.targets($feat-color) {
        opacity: $opacity;
      }
    }
  }
}

@mixin width($width, $query: feature-targeting.all()) {
  $feat-structure: feature-targeting.create-target($query, structure);

  @include feature-targeting.targets($feat-structure) {
    @if math.is-unitless($width) {
      width: $width * variables.$width-base;
    } @else {
      width: $width;
    }
  }
}

/// Sets the min-width of the menu.
/// @param {Number} $min-width - the desired min-width.
@mixin min-width($min-width, $query: feature-targeting.all()) {
  $feat-structure: feature-targeting.create-target($query, structure);

  @include feature-targeting.targets($feat-structure) {
    $min-width-custom-property: custom-properties.create(
      menu-custom-properties.$min-width,
      $min-width
    );
    @include theme.property(min-width, $min-width-custom-property);
  }
}
