@use 'sass:list';
@use "sass:meta";
@use '../../00-settings/vars' as *;

////
///
/// This file defines breakpoint related mixins / functions.
///
/// @group breakpoints
/// @author Lennart Van Vaerenbergh
///

/// Parse a single condition list like (min-width 768px) or (max-width 767px).
/// Returns a map (op: min-width|max-width, val: <length>) or null.
@function _bp-parse-cond($item) {
  @if meta.type-of($item) == 'list' and list.length($item) == 2 {
    $op: list.nth($item, 1);
    $val: list.nth($item, 2);

    @if $op == min-width {
      @return ($val, null);
    }
    @else if $op == max-width {
      @return (null, $val);
    }
  }

  // Fallback ensures callers always receive a 2-item list.
  @return (null, null);
}

/// Flexible breakpoint mixin.
/// Supports:
/// - @include breakpoint(768px) → min-width.
/// - @include breakpoint(max-width 767px) or wrapped as ((max-width 767px)).
/// - @include breakpoint((min-width 480px) (max-width 767px)) → range.
/// Always end comment lines with a full stop.
@mixin breakpoint($args...) {
  $argc: list.length($args);
  $min: null;
  $max: null;

  // Case A: @include breakpoint(768px).
  @if $argc == 1 and meta.type-of(list.nth($args, 1)) != 'list' {
    $min: list.nth($args, 1);

    // Case B: One list argument: either a single condition or a range of two conditions.
  }
  @else if $argc == 1 and meta.type-of(list.nth($args, 1)) == 'list' {
    $a: list.nth($args, 1);

    // Single condition like (max-width 767px).
    $single-minmax: _bp-parse-cond($a);
    @if list.nth($single-minmax, 1) != null or list.nth($single-minmax, 2) != null {
      $min: list.nth($single-minmax, 1);
      $max: list.nth($single-minmax, 2);

    }
    @else if list.length($a) == 2 {
      // Range like ((min-width 480px) (max-width 767px)).
      $c1: _bp-parse-cond(list.nth($a, 1));
      $c2: _bp-parse-cond(list.nth($a, 2));
      $min: list.nth($c2, 1);
      @if list.nth($c1, 1) != null {
         $min: list.nth($c1, 1);
      }
      $max: list.nth($c2, 2);
      @if list.nth($c1, 2) != null {
        $max: list.nth($c1, 2);
      }

      @if ($min == null and $max == null) or (meta.type-of($min) == 'string' and meta.type-of($max) == 'string') {
        @error 'Invalid range for mixin breakpoint. Use ((min-width 480px) (max-width 1024px)).';
      }

    }
    @else {
      @error 'Invalid arguments for mixin breakpoint. Use (768px), (max-width 1024px), or ((min-width 480px) (max-width 1024px)).';
    }

    // Case C: Two positional args like: @include breakpoint(max-width 767px).
  }
  @else if $argc == 2 and list.nth($args, 1) == max-width {
    $max: list.nth($args, 2);

  }
  @else {
    @error 'Invalid arguments for mixin breakpoint. Use (768px), (max-width 1024px), or ((min-width 480px) (max-width 1024px)).';
  }

  // Emit exactly one @media block based on what we parsed.
  @if $min != null and $max != null {
    @media screen and (min-width: $min) and (max-width: $max) {
      @content;
    }
  }
  @else if $min != null {
    @media screen and (min-width: $min) {
      @content;
    }
  }
  @else if $max != null {
    @media screen and (max-width: $max) {
      @content;
    }
  }
  @else {
    @error 'No valid breakpoint could be determined. Check your arguments.';
  }
}

///
/// Mobile breakpoint (see $bp-mobile defined in _vars.scss).
/// This assumes a min-width as defined by the variable.
///
/// @since 3.0.0
/// @group breakpoints
/// @access public
/// @author Gert-Jan Meire
/// @require $bp-mobile
///
@mixin mobile {
  @include breakpoint($bp-mobile) {
    @content;
  }
}

///
/// Phablet breakpoint (see $bp-phablet defined in _vars.scss).
/// This assumes a min-width as defined by the variable.
///
/// @since 3.0.0
/// @group breakpoints
/// @access public
/// @author Bart Delrue
/// @require $bp-phablet
///
@mixin phablet {
  @include breakpoint($bp-phablet) {
    @content;
  }
}

///
/// Tablet breakpoint (see $bp-tablet defined in _vars.scss).
/// This assumes a max-width as defined by the variable.
///
/// @since 3.0.0
/// @group breakpoints
/// @access public
/// @author Bart Delrue
/// @require $bp-tablet
///
@mixin tablet-and-below {
  @include breakpoint(max-width $bp-tablet - 1) {
    @content;
  }
}

///
/// Tablet breakpoint (see $bp-tablet defined in _vars.scss).
/// This assumes a min-width as defined by the variable.
///
/// @since 3.0.0
/// @group breakpoints
/// @access public
/// @author Gert-Jan Meire
/// @require $bp-tablet
///
@mixin tablet {
  @include breakpoint($bp-tablet) {
    @content;
  }
}

///
/// Mobile breakpoint (see $bp-mobile defined in _vars.scss).
/// This assumes a min-width as defined by the variable.
///
/// @since 3.0.0
/// @group breakpoints
/// @access public
/// @author Gert-Jan Meire
/// @require $bp-mobile
///
@mixin desktop-and-below {
  @include breakpoint(max-width $bp-desktop - 1) {
    @content;
  }
}

///
/// Desktop breakpoint (see $bp-tablet defined in _vars.scss).
/// This assumes a min-width as defined by the variable.
///
/// @since 3.0.0
/// @group breakpoints
/// @access public
/// @author Gert-Jan Meire
/// @require $bp-desktop
///
@mixin desktop {
  @include breakpoint($bp-desktop) {
    @content;
  }
}

///
/// Desktop XL breakpoint (see $bp-desktop-xl defined in _vars.scss).
/// This assumes a min-width as defined by the variable.
///
/// @since 3.0.0
/// @group breakpoints
/// @access public
/// @author Gert-Jan Meire
/// @require $bp-desktop-xl
///
@mixin desktop-xl {
  @include breakpoint($bp-desktop-xl) {
    @content;
  }
}
