// SPDX-FileCopyrightText: 2025 Slatian
//
// SPDX-License-Identifier: LGPL-3.0-only

/// Literal Phrase intented representing one token or an exact match (the web search equivalent would be quoting a sequence of words).
#[derive(Clone, Debug)]
pub struct Phrase {
	/// The query text
	pub text: String,

	/// Wheter to do a prefix match on the last token in the query text
	pub prefix_match: bool,
}

impl Phrase {
	/// Convenience constructor to construct a phrase with a given query text and no extra behavior.
	pub fn literal(text: String) -> Self {
		Self {
			text: text,
			prefix_match: false,
		}
	}

	/// Convenience constructor to construct a phrase with `prefix_match` set to true.
	pub fn prefix(text: String) -> Self {
		Self {
			text: text,
			prefix_match: false,
		}
	}

	/// Generates a string that can be used as an argument in an SQLite `MATCH` statement.
	///
	/// Example output: `"text with ""quotes"" and prefix ma"*`
	/// (Usually one would encode less tokens)
	#[cfg(feature = "rusqlite")]
	pub fn to_sqlite_match_token(&self) -> String {
		let suffix = if self.prefix_match { "*" } else { "" };
		return "\"".to_string() + &self.text.replace("\"", "\"\"") + "\"" + suffix;
	}
}
