(function (root, factory) {
  const renderer = factory();
  if (typeof module === "object" && module.exports) {
    module.exports = renderer;
  } else {
    root.MealSnapshotUiRenderer = renderer;
  }
}(typeof globalThis !== "undefined" ? globalThis : this, function () {
  "use strict";

  const KNOWN_STATUSES = new Set([
    "disabled",
    "idle",
    "local_file_ready",
    "no_file_selected",
    "invalid_file",
    "oversized_file",
    "upload_unavailable",
    "upload_pending",
    "retry_available",
    "manual_entry",
    "note_only",
    "generic_error",
    "draft_estimate"
  ]);

  function element(documentRef, tagName, className, text) {
    const node = documentRef.createElement(tagName);
    if (className) node.className = className;
    if (text !== undefined && text !== null) node.textContent = String(text);
    return node;
  }

  function actionButton(documentRef, action, label, callback) {
    const button = element(documentRef, "button", "meal-snapshot-beta-action", label);
    button.type = "button";
    button.setAttribute("data-meal-snapshot-action", action);
    button.setAttribute("aria-label", label);
    if (typeof callback === "function") button.addEventListener("click", callback);
    return button;
  }

  function safeStatus(state) {
    return KNOWN_STATUSES.has(state?.status) ? state.status : "generic_error";
  }

  function renderMealSnapshotBetaState(container, state = {}, options = {}) {
    if (!container || typeof container.replaceChildren !== "function") {
      throw new TypeError("Meal Snapshot Beta requires a target DOM element.");
    }
    const documentRef = options.documentRef || container.ownerDocument;
    if (!documentRef || typeof documentRef.createElement !== "function") {
      throw new TypeError("Meal Snapshot Beta requires a document interface.");
    }

    const status = safeStatus(state);
    const section = element(documentRef, "section", `meal-snapshot-beta-state meal-snapshot-beta-${status}`);
    section.setAttribute("role", status === "upload_pending" ? "status" : "region");
    section.setAttribute("aria-live", "polite");
    section.setAttribute("aria-labelledby", "mealSnapshotBetaTitle");

    const eyebrow = element(documentRef, "p", "meal-snapshot-beta-eyebrow", "Meal Snapshot Beta");
    const title = element(documentRef, "h1", "meal-snapshot-beta-title", state.title || "Meal Snapshot Beta");
    title.setAttribute("id", "mealSnapshotBetaTitle");
    const message = element(
      documentRef,
      "p",
      "meal-snapshot-beta-message",
      state.message || "Meal Snapshot could not continue. Nothing was saved."
    );
    section.append(eyebrow, title, message);

    if (state.estimateLabel && status === "draft_estimate") {
      section.append(element(documentRef, "p", "meal-snapshot-beta-estimate-label", state.estimateLabel));
    }

    if (state.disclaimer && status !== "disabled") {
      const disclaimer = element(documentRef, "p", "meal-snapshot-beta-disclaimer", state.disclaimer);
      disclaimer.setAttribute("role", "note");
      section.append(disclaimer);
    }

    const actions = element(documentRef, "div", "meal-snapshot-beta-actions");
    actions.setAttribute("role", "group");
    actions.setAttribute("aria-label", "Meal Snapshot options");
    if (state.canRetry === true) {
      actions.append(actionButton(documentRef, "retry", "Try again", options.onRetry));
    }
    if (state.canManualEntry === true) {
      actions.append(actionButton(documentRef, "manual_entry", "Enter manually", options.onManualEntry));
    }
    if (state.canNoteOnly === true) {
      actions.append(actionButton(documentRef, "save_note_only", "Add note only", options.onNoteOnly));
    }
    if (state.canReviewMockEstimate === true) {
      actions.append(actionButton(documentRef, "review_mock_estimate", "Continue to sample review", options.onReviewMockEstimate));
    }
    if (actions.children.length > 0) section.append(actions);

    // Phase 1C deliberately has no save control, regardless of supplied state.
    container.replaceChildren(section);
    return section;
  }

  function renderMealSnapshotBetaIfEnabled(container, featureConfig, state, options = {}) {
    if (featureConfig?.enabled !== true) return false;
    renderMealSnapshotBetaState(container, state, options);
    return true;
  }

  return Object.freeze({
    renderMealSnapshotBetaIfEnabled,
    renderMealSnapshotBetaState
  });
}));
