using System.Text.Json.Serialization;
namespace Flow.Launcher.Plugin
{
///
/// Represents a query that is sent to a plugin.
///
public class Query
{
///
/// Raw query, this includes action keyword if it has
/// We didn't recommend use this property directly. You should always use Search property.
///
public string RawQuery { get; internal init; }
///
/// Determines whether the query was forced to execute again.
/// For example, the value will be true when the user presses Ctrl + R.
/// When this property is true, plugins handling this query should avoid serving cached results.
///
public bool IsReQuery { get; internal set; } = false;
///
/// Search part of a query.
/// This will not include action keyword if exclusive plugin gets it, otherwise it should be same as RawQuery.
/// Since we allow user to switch a exclusive plugin to generic plugin,
/// so this property will always give you the "real" query part of the query
///
public string Search { get; internal init; }
///
/// The search string split into a string array.
/// Does not include the .
///
public string[] SearchTerms { get; init; }
///
/// Query can be splited into multiple terms by whitespace
///
public const string TermSeparator = " ";
///
/// User can set multiple action keywords seperated by whitespace
///
public const string ActionKeywordSeparator = TermSeparator;
///
/// Wildcard action keyword. Plugins using this value will be queried on every search.
///
public const string GlobalPluginWildcardSign = "*";
///
/// The action keyword part of this query.
/// For global plugins this value will be empty.
///
public string ActionKeyword { get; init; }
///
/// Splits by spaces and returns the first item.
///
///
/// returns an empty string when does not have enough items.
///
[JsonIgnore]
public string FirstSearch => SplitSearch(0);
[JsonIgnore]
private string _secondToEndSearch;
///
/// strings from second search (including) to last search
///
[JsonIgnore]
public string SecondToEndSearch => SearchTerms.Length > 1 ? (_secondToEndSearch ??= string.Join(' ', SearchTerms[1..])) : "";
///
/// Splits by spaces and returns the second item.
///
///
/// returns an empty string when does not have enough items.
///
[JsonIgnore]
public string SecondSearch => SplitSearch(1);
///
/// Splits by spaces and returns the third item.
///
///
/// returns an empty string when does not have enough items.
///
[JsonIgnore]
public string ThirdSearch => SplitSearch(2);
private string SplitSearch(int index)
{
return index < SearchTerms.Length ? SearchTerms[index] : string.Empty;
}
///
public override string ToString() => RawQuery;
}
}