/**
* Validation for Arma 3 Launcher preset exports (the only accepted mod list
* format). Pure module (client-safe): the server action treats it as
* authoritative, and CreateSideOpDialog uses it for instant client-side
* feedback before the upload starts.
*
* A launcher export is an HTML document carrying ``, an optional ``, and one
* `
` row per mod. We validate those structural
* markers rather than the generator/footer strings, which third-party launcher
* clones rewrite.
*/
export type ArmaPresetFailureReason = "notPreset" | "empty";
export type ArmaPresetSummary =
| { ok: true; presetName: string | null; modCount: number }
| { ok: false; reason: ArmaPresetFailureReason };
const META_TAG_RE = /]*>/gi;
const NAME_ATTR_RE = /\bname\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/i;
const CONTENT_ATTR_RE = /\bcontent\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/i;
const MOD_ROW_RE = /\bdata-type\s*=\s*(?:"ModContainer"|'ModContainer'|ModContainer)/gi;
function attr(tag: string, attribute: RegExp): string | null {
const match = tag.match(attribute);
if (!match) return null;
return (match[1] ?? match[2] ?? match[3] ?? "").trim();
}
/**
* Inspect a candidate file's text. Returns the preset name and mod count when
* the content matches the launcher export format, otherwise a failure reason:
* - `notPreset`: not an HTML Arma 3 preset export at all (wrong file type).
* - `empty`: a valid preset containing zero mod entries.
*/
export function parseArmaPreset(source: string): ArmaPresetSummary {
const html = source.replace(/^\uFEFF/, "");
if (!/