/**
 * Shortcode-string builder for the Get Shortcode modal, with WP bracket syntax. Defaults are OMITTED
 * so the generated shortcode stays minimal; only `trigger` is quoted (embedded
 * double quotes stripped). Attribute names mirror
 * app/Frontend/Shortcode_Renderer's shortcode_atts contract exactly.
 */

export function defaultShortcodeState(id) {
    return {
        id: Number(id) || 0,
        modal: false,
        trigger: "",
        theme: "auto",
        width: 680,
        show_title: false,
        auto_close: false,
        auto_close_timeout: 3,
        auto_open: false,
        auto_open_after: null,
        auto_open_after_scroll: null,
        frequency: "once-per-session",
    };
}

export function buildShortcode(state) {
    const id = Number(state.id) || 0;
    if (id <= 0) {
        return "";
    }

    const parts = [`id="${id}"`];

    if (!state.modal) {
        return `[formatrica ${parts.join(" ")}]`;
    }

    parts.push("modal=1");

    const trigger = String(state.trigger || "").trim();
    if (trigger !== "") {
        parts.push(`trigger="${trigger.replace(/"/g, "")}"`);
    }
    if (state.theme && state.theme !== "auto") {
        parts.push(`theme=${state.theme}`);
    }
    const width = Number(state.width);
    if (Number.isFinite(width) && width > 0 && width !== 680) {
        parts.push(`width=${width}`);
    }
    if (state.show_title) {
        parts.push("show-title=1");
    }
    if (state.auto_close) {
        parts.push("auto-close=1");
        const timeout = Number(state.auto_close_timeout);
        if (Number.isFinite(timeout) && timeout >= 0 && timeout !== 3) {
            parts.push(`auto-close-timeout=${timeout}`);
        }
    }
    if (state.auto_open) {
        parts.push("auto-open=1");
    }
    const after = Number(state.auto_open_after);
    if (Number.isFinite(after) && after > 0) {
        parts.push(`auto-open-after=${after}`);
    }
    const scroll = Number(state.auto_open_after_scroll);
    if (Number.isFinite(scroll) && scroll > 0) {
        parts.push(`auto-open-after-scroll=${scroll}`);
    }
    if (state.frequency && state.frequency !== "once-per-session") {
        parts.push(`frequency=${state.frequency}`);
    }

    return `[formatrica ${parts.join(" ")}]`;
}
