import { BaseItemSheetL5r5e } from "./base-item-sheet.js";

/**
 * Sheet for Arcane items (Chiaroscuro).
 * @extends {BaseItemSheetL5r5e}
 */
export class ArcaneSheetL5r5e extends BaseItemSheetL5r5e {
    /** @override */
    static get defaultOptions() {
        return foundry.utils.mergeObject(super.defaultOptions, {
            classes: ["l5r5e", "sheet", "arcane"],
            template: CONFIG.l5r5e.paths.templates + "items/arcane/arcane-sheet.html",
            width: 500,
            height: 400,
        });
    }

    /** @override */
    async getData(options = {}) {
        const sheetData = await super.getData(options);

        // Convert application array to comma-separated string for display
        const app = sheetData.data.system.application;
        sheetData.data.system.applicationDisplay = Array.isArray(app) ? app.join(", ") : (app ?? "");

        sheetData.data.enrichedHtml = {
            description: await foundry.applications.ux.TextEditor.implementation.enrichHTML(
                sheetData.data.system.description ?? "",
                { async: true }
            ),
        };

        return sheetData;
    }

    /** @override */
    async _updateObject(event, formData) {
        // Convert comma-separated application string back to array
        const raw = formData["system.applicationDisplay"] ?? "";
        formData["system.application"] = raw.split(",").map((s) => s.trim()).filter(Boolean);
        delete formData["system.applicationDisplay"];

        return super._updateObject(event, formData);
    }
}
