// Copyright (c) 2025 MatteuSan and its affiliates.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.

@use 'sass:list';
@use 'sass:map';
@use 'sass:meta';

/// Check if query is empty.
/// @param {*} $query
/// @return {boolean} true if empty, false if not.
@function is-empty($query) {
  @if not $query
       or $query == ''
       or $query == ()
       or $query == none
       or $query == null
       or list.length($query) == 0 {
    @return true;
  }
  @return false;
}

/// Validate a query's data type from a reference.
/// @param {string} $reference
/// @param {*} $query
/// @param {boolean} $errors
/// @param {string} $source
/// @return {boolean|error} true if the query matches the referenced data type.
@function is-type($reference, $query, $errors: true, $source: ()) {
  @if meta.type-of($query) != unquote($reference) {
    @if $errors {
      @error 'ERROR [#{$source}]: Invalid #{$reference}: #{$query}. Expecting a #{$reference}';
    }
    @return false;
  }
  @return true;
}

/// Checks if the list has the item queried.
/// @param {list} $reference
/// @param {*} $query
/// @return {*} an entity if item is successfully indexed from a list.
@function has-item($reference, $query) {
  @return list.index($reference, $query) != null;
}

/// Checks if the map has the property queried.
/// @param {list} $reference
/// @param {*} $query
/// @return {*} an entity if item is successfully indexed from a map.
@function has-property($reference, $query) {
  @return map.has-key($reference, $query) != null;
}