/*
  This file is part of TALER
  (C) 2026 Taler Systems SA

  TALER is free software; you can redistribute it and/or modify it under the
  terms of the GNU Lesser General Public License as published by the Free Software
  Foundation; either version 3, or (at your option) any later version.

  TALER 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
  TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
*/
/**
 * @file src/util/util.c
 * @brief small helpers
 * @author Christian Grothoff
 */
#include "platform.h"
#include <string.h>
#include "taler/taler_merchant_util.h"
#include <taler/taler_json_lib.h>
#include <gnunet/gnunet_json_lib.h>


bool
TALER_MERCHANT_taxes_array_valid (const json_t *taxes)
{
  json_t *tax;
  size_t idx;

  if (! json_is_array (taxes))
    return false;
  json_array_foreach (taxes, idx, tax)
  {
    struct TALER_Amount amount;
    const char *name;
    struct GNUNET_JSON_Specification spec[] = {
      GNUNET_JSON_spec_string ("name",
                               &name),
      TALER_JSON_spec_amount_any ("tax",
                                  &amount),
      GNUNET_JSON_spec_end ()
    };
    enum GNUNET_GenericReturnValue res;
    const char *ename;
    unsigned int eline;

    res = GNUNET_JSON_parse (tax,
                             spec,
                             &ename,
                             &eline);
    if (GNUNET_OK != res)
    {
      GNUNET_break_op (0);
      return false;
    }
  }
  return true;
}


enum TALER_MERCHANT_ContractTokenKind
TALER_MERCHANT_contract_token_kind_from_string (const char *str)
{
  if (NULL == str)
  {
    GNUNET_break (0);
    return TALER_MERCHANT_CONTRACT_TOKEN_KIND_INVALID;
  }
  if (0 == strcmp ("subscription",
                   str))
    return TALER_MERCHANT_CONTRACT_TOKEN_KIND_SUBSCRIPTION;
  if (0 == strcmp ("discount",
                   str))
    return TALER_MERCHANT_CONTRACT_TOKEN_KIND_DISCOUNT;
  return TALER_MERCHANT_CONTRACT_TOKEN_KIND_INVALID;
}
