@use "sass:list";
@use "sass:map";
@use "list" as list-utils;
@use "media";
@use "mode";
@use "option";

//== Args ======================================================================
$win:    "win";
$mac:    "mac";
$linux:  "linux";

$win7:   "win7";
$win8:   "win8";
$win10:  "win10";
$bigsur: "bigsur";

//== Privated Variables ========================================================
$_win-template: "-moz-os-version: windows" "-moz-platform: windows";
$_win-media: (
  #{$win7}:  "win7",
  #{$win8}:  "win8",
  #{$win10}: "win10",
);
@each $_win-key in map.keys($_win-media) {
  $_win-value: map.get($_win-media, $_win-key);

  $result-template: ();
  @for $i from 1 through list.length($_win-template) {
    $result-value: list.nth($_win-template, $i) + "-" + $_win-value;
    $result-template: list.append($result-template, $result-value);
  }
  $_win-media: map.set($_win-media, $_win-key, $result-template);
}

$_moz-platform-windows: list.nth($_win-template, 2);
@function _win-only-result() {
  $result-template: ();

  @each $_win-value in map.values($_win-media) {
    $result-template: list.append($result-template, list.nth($_win-value, 1));
  }
  @return list.append($result-template, $_moz-platform-windows)
}

$_win-media: map.merge(
  $_win-media,
  (#{$win}: _win-only-result())
);

$_others-media: (
  #{$linux}: "-moz-gtk-csd-available",
  #{$bigsur}: "-moz-mac-big-sur-theme: 0",
);
$_os-media: map.merge(
  $_win-media, $_others-media
);

//== Mixin =====================================================================
@mixin OS($osNames...) {
  $_os-media: $_os-media; // as local
  @if mode.isSTANDARD() {
    $osNames: list-utils.remove-list($osNames, ($win7 $win8));
    $_os-media: map.set($_os-media, $win10, $_moz-platform-windows);
    $_os-media: map.set($_os-media, $win, $_moz-platform-windows);
  }

  $osMedias: ();
  @each $osName in $osNames {
    $osMedia: map.get($_os-media, $osName);
    @if $osMedia != null {
      $osMedias: list.join($osMedias, $osMedia);
    }
  }

  @include media.each($osMedias...) {
    @content;
  }

  @if list.index($osNames, $mac) {
    @include option.Option("layout.css.osx-font-smoothing.enabled") {
      @content;
    }
  }
}

@mixin WinClassic() {
  @if mode.isESR() {
    @media (-moz-windows-classic) {
      @content;
    }
  }
}