/*
 * Copyright (c) 2026 Jonathan Schleifer <js@nil.im>
 *
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3.0 only,
 * as published by the Free Software Foundation.
 *
 * 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 Lesser General Public License
 * version 3.0 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3.0 along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

#ifndef GEMTEXT2HTML_G2H_CONVERTER_H
#define GEMTEXT2HTML_G2H_CONVERTER_H

/** @file */

#ifdef __OBJC__
# import <ObjFW/ObjFW.h>

/**
 * @class G2HConverter G2HConverter.h Gemtext2HTML/Gemtext2HTML.h
 *
 * @brief A converter that processes Gemtext to HTML line-by-line.
 */
@interface G2HConverter: OFObject
{
	bool _preformatted, _previousIsHeading, _previousIsListItem;
	bool _previousIsQuote;
	OFSet OF_GENERIC(OFString *) *_imageExtensions;
	OFString *_localLinkEmoji, *_externalLinkEmoji, *_lastReturnedString;
	OFString *_localLinkPrefix, *_emailLinkEmoji;
}

/**
 * @brief The extensions (of IRI paths) that should be recognized as an image.
 *
 * Defaults to `gif`, `jpeg`, `jpg`, `png`, `qoi` and `svg`.
 */
@property (copy, nonatomic) OFSet OF_GENERIC(OFString *) *imageExtensions;

/**
 * @brief Prefix for local links.
 *
 * Defaults to the empty string.
 */
@property (copy, nonatomic) OFString *localLinkPrefix;

/**
 * @brief The emoji to prefix local links with.
 *
 * Defaults to `👉`.
 */
@property (copy, nonatomic) OFString *localLinkEmoji;

/**
 * @brief The emoji to prefix external links with.
 *
 * Defaults to `🌐`.
 */
@property (copy, nonatomic) OFString *externalLinkEmoji;

/**
 * @brief The emoji to prefix e-mail links with.
 *
 * Defaults to `📧`.
 */
@property (copy, nonatomic) OFString *eMailLinkEmoji;

/**
 * @brief Creates a new converter.
 *
 * @return A new, autoreleased converter.
 */
+ (instancetype)converter;

/**
 * @brief Processes a single line of Gemtext.
 *
 * @param line The Gemtext line to process
 * @return HTML for the specified Gemtext line
 */
- (OFString *)processLine: (OFString *)line;

/**
 * @brief Finishes processing.
 *
 * @return Additional HTML to append to close tags that were opened before
 */
- (OFString *)finishProcessing;
@end
#else
# include <stdbool.h>

typedef void G2HConverter;
#endif

#ifdef __cplusplus
extern "C" {
#endif
/**
 * @brief Creates a new Gemtext2HTML converter.
 *
 * Use @ref g2h_converter_free to free it and its resources.
 *
 * @return A new Gemtext2HTML converter or `NULL` on error.
 */
extern G2HConverter *g2h_converter_new(void);

/**
 * @brief Frees the specified Gemtext2HTML converter and its resources
 *
 * @param converter The converter which to free
 */
extern void g2h_converter_free(G2HConverter *converter);

/**
 * @brief Sets the extensions (of IRI paths) that should be recognized as an
 *	  image.
 *
 * Defaults to `gif`, `jpeg`, `jpg`, `png`, `qoi` and `svg`.
 *
 * @param converter The converter for which to set the image extensions
 * @param imageExtensions The image extensions to set. This is an array of C
 *			  strings terminated by `NULL`.
 * @return Whether the image extensions were successfully set
 */
extern bool g2h_converter_set_image_extensions(G2HConverter *converter,
    const char *imageExtensions[]);

/**
 * @brief Sets prefix for local links.
 *
 * Defaults to the empty string.
 *
 * @param converter The converter for which to set the local link prefix
 * @param localLinkPrefix Prefix for local links
 * @return Whether the local link prefix was successfully set
 */
extern bool g2h_converter_set_local_link_prefix(G2HConverter *converter,
    const char *localLinkPrefix);

/**
 * @brief Sets the emoji to prefix local links with.
 *
 * Defaults to `👉`.
 *
 * @param converter The converter for which to set the local link emoji
 * @param localLinkEmoji The emoji to prefix local links with
 * @return Whether the local link emoji was successfully set
 */
extern bool g2h_converter_set_local_link_emoji(G2HConverter *converter,
    const char *localLinkEmoji);

/**
 * @brief Sets the emoji to prefix external links with.
 *
 * Defaults to `🌐`.
 *
 * @param converter The converter for which to set the external link emoji
 * @param externalLinkEmoji The emoji to prefix external links with
 * @return Whether the external link emoji was successfully set
 */
extern bool g2h_converter_set_external_link_emoji(G2HConverter *converter,
    const char *externalLinkEmoji);

/**
 * @brief Sets the emoji to prefix e-mail links with.
 *
 * Defaults to `📧`.
 *
 * @param converter The converter for which to set the e-mail link emoji
 * @param eMailLinkEmoji The emoji to prefix e-mail links with
 * @return Whether the e-mail link emoji was successfully set
 */
extern bool g2h_converter_set_email_link_emoji(G2HConverter *converter,
    const char *eMailLinkEmoji);

/**
 * @brief Processes a single line of Gemtext.
 *
 * @param converter The converter to process a single line of Gemtext with
 * @param line The Gemtext line to process
 * @return HTML for the specified Gemtext line. This pointer is valid until the
 *	   next line is processed or the converter is freed. Returns `NULL` on
 *	   error.
 */
extern const char *g2h_converter_process_line(G2HConverter *converter,
    const char *line);

/**
 * @brief Finishes processing.
 *
 * @return Additional HTML to append to close tags that were opened before or
 *	   NULL on error
 */
extern const char *g2h_converter_finish_processing(G2HConverter *converter);
#ifdef __cplusplus
}
#endif

#endif
