/* src/bazaar.p/bz-search-bias.c
 *
 * Copyright 2026 AUTOGEN
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#include "src/bazaar.p/bz-search-bias.h"



struct _BzSearchBias
{
  GObject parent_instance;

  char *regex;
  char *convert_to;
  BzLinearFunction *linear_boost;
  BzExponentialFunction *exponential_boost;
  GListModel *boost_appids;
};

G_DEFINE_FINAL_TYPE (BzSearchBias, bz_search_bias, G_TYPE_OBJECT);

enum
{
  PROP_0,

  PROP_REGEX,
  PROP_CONVERT_TO,
  PROP_LINEAR_BOOST,
  PROP_EXPONENTIAL_BOOST,
  PROP_BOOST_APPIDS,

  LAST_PROP
};
static GParamSpec *props[LAST_PROP] = { 0 };

static void
bz_search_bias_dispose (GObject *object)
{
  BzSearchBias *self = BZ_SEARCH_BIAS (object);

  g_clear_pointer (&self->regex, g_free);
  g_clear_pointer (&self->convert_to, g_free);
  g_clear_pointer (&self->linear_boost, g_object_unref);
  g_clear_pointer (&self->exponential_boost, g_object_unref);
  g_clear_pointer (&self->boost_appids, g_object_unref);

  G_OBJECT_CLASS (bz_search_bias_parent_class)->dispose (object);
}

static void
bz_search_bias_get_property (GObject    *object,
                       guint       prop_id,
                       GValue     *value,
                       GParamSpec *pspec)
{
  BzSearchBias *self = BZ_SEARCH_BIAS (object);

  switch (prop_id)
    {
    case PROP_REGEX:
      g_value_set_string (value, bz_search_bias_get_regex (self));
      break;
    case PROP_CONVERT_TO:
      g_value_set_string (value, bz_search_bias_get_convert_to (self));
      break;
    case PROP_LINEAR_BOOST:
      g_value_set_object (value, bz_search_bias_get_linear_boost (self));
      break;
    case PROP_EXPONENTIAL_BOOST:
      g_value_set_object (value, bz_search_bias_get_exponential_boost (self));
      break;
    case PROP_BOOST_APPIDS:
      g_value_set_object (value, bz_search_bias_get_boost_appids (self));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
bz_search_bias_set_property (GObject      *object,
                       guint         prop_id,
                       const GValue *value,
                       GParamSpec   *pspec)
{
  BzSearchBias *self = BZ_SEARCH_BIAS (object);

  switch (prop_id)
    {
    case PROP_REGEX:
      bz_search_bias_set_regex (self, g_value_get_string (value));
      break;
    case PROP_CONVERT_TO:
      bz_search_bias_set_convert_to (self, g_value_get_string (value));
      break;
    case PROP_LINEAR_BOOST:
      bz_search_bias_set_linear_boost (self, g_value_get_object (value));
      break;
    case PROP_EXPONENTIAL_BOOST:
      bz_search_bias_set_exponential_boost (self, g_value_get_object (value));
      break;
    case PROP_BOOST_APPIDS:
      bz_search_bias_set_boost_appids (self, g_value_get_object (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
bz_search_bias_class_init (BzSearchBiasClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);



  object_class->set_property = bz_search_bias_set_property;
  object_class->get_property = bz_search_bias_get_property;
  object_class->dispose      = bz_search_bias_dispose;

  props[PROP_REGEX] =
      g_param_spec_string (
          "regex",
          NULL, NULL, NULL,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  props[PROP_CONVERT_TO] =
      g_param_spec_string (
          "convert-to",
          NULL, NULL, NULL,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  props[PROP_LINEAR_BOOST] =
      g_param_spec_object (
          "linear-boost",
          NULL, NULL,
          BZ_TYPE_LINEAR_FUNCTION,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  props[PROP_EXPONENTIAL_BOOST] =
      g_param_spec_object (
          "exponential-boost",
          NULL, NULL,
          BZ_TYPE_EXPONENTIAL_FUNCTION,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  props[PROP_BOOST_APPIDS] =
      g_param_spec_object (
          "boost-appids",
          NULL, NULL,
          G_TYPE_LIST_MODEL,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  g_object_class_install_properties (object_class, LAST_PROP, props);
}

static void
bz_search_bias_init (BzSearchBias *self)
{
}

BzSearchBias *
bz_search_bias_new (void){
  return g_object_new (BZ_TYPE_SEARCH_BIAS, NULL);
}

const char *
bz_search_bias_get_regex (BzSearchBias *self){
  g_return_val_if_fail (BZ_IS_SEARCH_BIAS (self), NULL);
  return self->regex;
}

const char *
bz_search_bias_get_convert_to (BzSearchBias *self){
  g_return_val_if_fail (BZ_IS_SEARCH_BIAS (self), NULL);
  return self->convert_to;
}

BzLinearFunction *
bz_search_bias_get_linear_boost (BzSearchBias *self){
  g_return_val_if_fail (BZ_IS_SEARCH_BIAS (self), NULL);
  return self->linear_boost;
}

BzExponentialFunction *
bz_search_bias_get_exponential_boost (BzSearchBias *self){
  g_return_val_if_fail (BZ_IS_SEARCH_BIAS (self), NULL);
  return self->exponential_boost;
}

GListModel *
bz_search_bias_get_boost_appids (BzSearchBias *self){
  g_return_val_if_fail (BZ_IS_SEARCH_BIAS (self), NULL);
  return self->boost_appids;
}

void
bz_search_bias_set_regex (BzSearchBias *self,
    const char *regex){
  g_return_if_fail (BZ_IS_SEARCH_BIAS (self));

  if (regex == self->regex || (regex != NULL && self->regex != NULL && g_strcmp0(regex, self->regex) == 0))
    return;

  g_clear_pointer (&self->regex, g_free);
  if (regex != NULL)
    self->regex = g_strdup (regex);

  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_REGEX]);
}

void
bz_search_bias_set_convert_to (BzSearchBias *self,
    const char *convert_to){
  g_return_if_fail (BZ_IS_SEARCH_BIAS (self));

  if (convert_to == self->convert_to || (convert_to != NULL && self->convert_to != NULL && g_strcmp0(convert_to, self->convert_to) == 0))
    return;

  g_clear_pointer (&self->convert_to, g_free);
  if (convert_to != NULL)
    self->convert_to = g_strdup (convert_to);

  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_CONVERT_TO]);
}

void
bz_search_bias_set_linear_boost (BzSearchBias *self,
    BzLinearFunction *linear_boost){
  g_return_if_fail (BZ_IS_SEARCH_BIAS (self));

  if (linear_boost == self->linear_boost)
    return;

  g_clear_pointer (&self->linear_boost, g_object_unref);
  if (linear_boost != NULL)
    self->linear_boost = g_object_ref (linear_boost);

  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_LINEAR_BOOST]);
}

void
bz_search_bias_set_exponential_boost (BzSearchBias *self,
    BzExponentialFunction *exponential_boost){
  g_return_if_fail (BZ_IS_SEARCH_BIAS (self));

  if (exponential_boost == self->exponential_boost)
    return;

  g_clear_pointer (&self->exponential_boost, g_object_unref);
  if (exponential_boost != NULL)
    self->exponential_boost = g_object_ref (exponential_boost);

  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_EXPONENTIAL_BOOST]);
}

void
bz_search_bias_set_boost_appids (BzSearchBias *self,
    GListModel *boost_appids){
  g_return_if_fail (BZ_IS_SEARCH_BIAS (self));

  if (boost_appids == self->boost_appids)
    return;

  g_clear_pointer (&self->boost_appids, g_object_unref);
  if (boost_appids != NULL)
    self->boost_appids = g_object_ref (boost_appids);

  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_BOOST_APPIDS]);
}

void
bz_search_bias_set_regex_take (BzSearchBias *self,
    char *regex){
  g_return_if_fail (BZ_IS_SEARCH_BIAS (self));

  if (regex != NULL && self->regex != NULL && g_strcmp0(regex, self->regex) == 0){
    g_free (regex);
    return;
  }

  g_clear_pointer (&self->regex, g_free);
  if (regex != NULL)
    self->regex = regex;

  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_REGEX]);
}

void
bz_search_bias_set_convert_to_take (BzSearchBias *self,
    char *convert_to){
  g_return_if_fail (BZ_IS_SEARCH_BIAS (self));

  if (convert_to != NULL && self->convert_to != NULL && g_strcmp0(convert_to, self->convert_to) == 0){
    g_free (convert_to);
    return;
  }

  g_clear_pointer (&self->convert_to, g_free);
  if (convert_to != NULL)
    self->convert_to = convert_to;

  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_CONVERT_TO]);
}

/* End of src/bazaar.p/bz-search-bias.c */
