// Copyright 2016 Google Inc. Use of this source code is governed by an
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

import 'package:meta/meta.dart';
import 'package:source_span/source_span.dart';

import '../../../ast/sass.dart';
import '../../../visitor/interface/expression.dart';

/// A ternary expression.
///
/// This is defined as a separate syntactic construct rather than a normal
/// function because only one of the `$if-true` and `$if-false` arguments are
/// evaluated.
///
/// {@category AST}
final class LegacyIfExpression extends Expression
    implements CallableInvocation {
  /// The declaration of `if()`, as though it were a normal function.
  static final declaration = ParameterList.parse(
    r"@function if($condition, $if-true, $if-false) {",
  );

  /// The arguments passed to `if()`.
  final ArgumentList arguments;

  final FileSpan span;

  /// Returns a modern `if()` expression to use instead of this.
  ///
  /// @nodoc
  @internal
  String? get modernSuggestion => switch (arguments) {
        ArgumentList(
          positional: [var condition, var ifTrue, var ifFalse],
          named: Map(isEmpty: true),
          rest: null,
        ) =>
          ifFalse is NullExpression
              ? "if(sass($condition): $ifTrue)"
              : ifTrue is NullExpression
                  ? "if(not sass($condition): $ifFalse)"
                  : "if(sass($condition): $ifTrue; else: $ifFalse)",
        _ => null,
      };

  LegacyIfExpression(this.arguments, this.span);

  T accept<T>(ExpressionVisitor<T> visitor) =>
      visitor.visitLegacyIfExpression(this);

  String toString() => "if$arguments";
}
