// The RU <Text> description is edited as two fields but stored and pushed as ONE text:
//   « description courte » (1st paragraph → Airbnb summary, ≤ 500)
//   + blank line
//   + « complément Expedia » (→ Airbnb « The space », and the rest of Expedia's text).
// The blank line is the separator, so the short part must never contain a line break.

const SEPARATOR = '\n\n';

export type DescriptionParts = { short: string; complement: string };

/** Split the stored text at its first blank line. No blank line → everything is the short part. */
export function splitDescription(text: string | null | undefined): DescriptionParts {
    const value = (text ?? '').replace(/\r\n/g, '\n');
    const index = value.indexOf(SEPARATOR);
    if (index === -1) {
        return { short: value, complement: '' };
    }

    return { short: value.slice(0, index), complement: value.slice(index + SEPARATOR.length) };
}

/** Rebuild the single RU text. An empty complement adds no trailing separator. */
export function joinDescription(short: string, complement: string): string {
    return complement === '' ? short : short + SEPARATOR + complement;
}

/** The short part is one paragraph: typed or pasted line breaks become spaces. */
export function singleParagraph(value: string): string {
    return value.replace(/[ \t]*[\r\n]+[ \t]*/g, ' ');
}
