/* FichothequeLib_API - Copyright (c) 2026 Vincent Calame - Exemole
 * Logiciel libre donné sous triple licence :
 * 1) selon les termes de la CeCILL V2
 * 2) selon les termes de l’EUPL V.1.1
 * 3) selon les termes de la GNU GPLv3
 * Voir le fichier licences.txt
 */


package net.fichotheque.syntax;

import java.io.IOException;


/**
 *
 * @author Vincent Calame
 */
public class TypoConverter {

    private final String source;
    private final Appendable destination;
    private final char closingChar;

    public TypoConverter(String source, Appendable destination) {
        this.source = source;
        this.destination = destination;
        this.closingChar = 0;
    }

    public TypoConverter(String source, Appendable destination, char closingChar) {
        this.source = source;
        this.destination = destination;
        this.closingChar = closingChar;
    }

    public int convert(int index) throws IOException {
        char carac = source.charAt(index);
        if (carac == closingChar) {
            destination.append('\\');
            destination.append(closingChar);
            return 0;
        }
        switch (carac) {
            case '\u00A0':
                if (isNarrow(index)) {
                    destination.append("\\u00A0");
                } else {
                    destination.append('~');
                }
                return 0;
            case '\u202F':
                if (isNarrow(index)) {
                    destination.append('~');
                } else {
                    destination.append("\\u202F");
                }
                return 0;
            case '~':
                destination.append("\\~");
                return 0;
            case '\\':
                if (canBeEscapingAntislash(index)) {
                    destination.append("\\\\");
                    return 0;
                } else {
                    destination.append("\\");
                    return -1;
                }
            case '.':
                if (index < (source.length() - 2)) {
                    if ((source.charAt(index + 1) == '.') && (source.charAt(index + 2) == '.')) {
                        destination.append("\\...");
                        return 2;
                    }
                }
                return -1;
            default:
                return -1;
        }
    }

    private boolean isNarrow(int index) {
        if (index == (source.length() - 1)) {
            return false;
        }
        if (index > 0) {
            switch (source.charAt(index - 1)) {
                case '\u00AB': //«
                    return true;
            }
        }
        switch (source.charAt(index + 1)) {
            case '?':
            case '!':
            case ';':
            case '\u00BB': // »
                return true;
            default:
                return false;
        }
    }

    private boolean canBeEscapingAntislash(int index) {
        if (index == (source.length() - 1)) {
            return false;
        }
        switch (source.charAt(index + 1)) {
            case ']':
            case '}':
            case '\\':
            case '~':
                return true;
            case 'u':
                if (index < (source.length() - 2)) {
                    char firstDigit = source.charAt(index + 2);
                    if (Character.digit(firstDigit, 16) >= 0) {
                        return true;
                    }
                }
                return false;
            case '.':
                if (index < (source.length() - 3)) {
                    if ((source.charAt(index + 2) == '.') && (source.charAt(index + 3) == '.')) {
                        return true;
                    }
                }
                return false;
            default:
                return false;
        }
    }

    public static void escapeString(Appendable buf, String s) throws IOException {
        TypoConverter typoConverter = new TypoConverter(s, buf);
        int length = s.length();
        for (int i = 0; i < length; i++) {
            int jump = typoConverter.convert(i);
            if (jump < 0) {
                buf.append(s.charAt(i));
            } else {
                i = i + jump;
            }
        }
    }

    public static void escapeString(Appendable buf, String s, char closingChar) throws IOException {
        TypoConverter typoConverter = new TypoConverter(s, buf, closingChar);
        int length = s.length();
        for (int i = 0; i < length; i++) {
            int jump = typoConverter.convert(i);
            if (jump < 0) {
                buf.append(s.charAt(i));
            } else {
                i = i + jump;
            }
        }
    }

}
