////
/// Copyright (c) 2016-2020 Martin Donath <martin.donath@squidfunk.com>
///
/// 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 NON-INFRINGEMENT. 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
////

// ----------------------------------------------------------------------------
// Variables
// ----------------------------------------------------------------------------

///
/// Admonition flavours
///
$admonitions: (
  note:                       pencil $clr-blue-a200,
  abstract summary tldr:      text-subject $clr-light-blue-a400,
  info todo:                  information $clr-cyan-a700,
  tip hint important:         fire $clr-teal-a700,
  success check done:         check-circle $clr-green-a700,
  question help faq:          help-circle $clr-light-green-a700,
  warning caution attention:  alert $clr-orange-a400,
  failure fail missing:       close-circle $clr-red-a200,
  danger error:               flash-circle $clr-red-a400,
  bug:                        bug $clr-pink-a400,
  example:                    format-list-numbered $clr-deep-purple-a400,
  quote cite:                 format-quote-close $clr-grey
) !default;

// ----------------------------------------------------------------------------
// Rules: layout
// ----------------------------------------------------------------------------

// Icon definitions
:root {
  @each $names, $props in $admonitions {
    $name: nth($names, 1);
    $icon: nth($props, 1);

    // Inline icon through string-replace-loader in webpack
    --md-admonition-icon--#{$name}: svg-load("@mdi/svg/svg/#{$icon}.svg");
  }
}

// ----------------------------------------------------------------------------

// Scoped in typesetted content to match specificity of regular content
.md-typeset {

  // Admonition extension
  .admonition {
    margin: 1.5625em 0;
    padding: 0 px2rem(12px);
    overflow: hidden;
    font-size: ms(-1);
    page-break-inside: avoid;
    border-left: px2rem(4px) solid $clr-blue-a200;
    border-radius: px2rem(2px);
    box-shadow:
      0 px2rem(4px) px2rem(10px) hsla(0, 0%, 0%, 0.05),
      0 0           px2rem(1px)  hsla(0, 0%, 0%, 0.1);

    // Adjust for right-to-left languages
    [dir="rtl"] & {
      border-right: px2rem(4px) solid $clr-blue-a200;
      border-left: none;
    }

    // Hack: omit rendering errors for print
    @media print {
      box-shadow: none;
    }

    // Adjust spacing on last element
    html & > :last-child {
      margin-bottom: px2rem(12px);
    }

    // Adjust margin for nested admonition blocks
    .admonition {
      margin: 1em 0;
    }

    // Wrapper for scrolling on overflow
    .md-typeset__scrollwrap {
      margin: 1em px2rem(-12px);
    }

    // Data table wrapper, in case JavaScript is available
    .md-typeset__table {
      padding: 0 px2rem(12px);
    }
  }

  // Admonition title
  .admonition-title {
    position: relative;
    margin: 0 px2rem(-12px);
    padding: px2rem(8px) px2rem(12px) px2rem(8px) px2rem(40px);
    font-weight: 700;
    background-color: transparentize($clr-blue-a200, 0.9);

    // Adjust for right-to-left languages
    [dir="rtl"] & {
      padding: px2rem(8px) px2rem(40px) px2rem(8px) px2rem(12px);
    }

    // Reset spacing, if title is the only element
    html &:last-child {
      margin-bottom: 0;
    }

    // Icon
    &::before {
      position: absolute;
      left: px2rem(12px);
      width: px2rem(20px);
      height: px2rem(20px);
      background-color: $clr-blue-a200;
      mask-image: var(--md-admonition-icon--note);
      content: "";

      // Adjust for right-to-left languages
      [dir="rtl"] & {
        right: px2rem(12px);
        left: initial;
      }
    }

    // Reset code inside Admonition titles
    code {
      margin: initial;
      padding: initial;
      color: currentColor;
      background-color: transparent;
      border-radius: initial;
      box-shadow: none;
    }
  }
}

// ----------------------------------------------------------------------------
// Rules: flavours
// ----------------------------------------------------------------------------

@each $names, $props in $admonitions {
  $name: nth($names, 1);
  $tint: nth($props, 2);

  // Define base class
  .md-typeset .admonition.#{$name} {
    border-color: $tint;
  }

  // Define base class
  .md-typeset .#{$name} > .admonition-title {
    background-color: transparentize($tint, 0.9);

    // Icon
    &::before {
      background-color: $tint;
      mask-image: var(--md-admonition-icon--#{$name});
    }
  }

  // Define synonyms for base class
  @if length($names) > 1 {
    @for $n from 2 through length($names) {
      .#{nth($names, $n)} {
        @extend .#{$name};
      }
    }
  }
}