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

  const MEAL_SNAPSHOT_ESTIMATE_DISCLAIMER = "This is an estimate based on your photo. It may miss sauces, oils, ingredients, or portion sizes. Review and adjust before saving.";

  function isEnabled(featureConfig) {
    return featureConfig?.enabled === true;
  }

  function uiState(status, options = {}) {
    return Object.freeze({
      status,
      title: options.title || "Meal Snapshot Beta",
      message: options.message || "Use this as a starting point and review anything that looks off.",
      canRetry: options.canRetry === true,
      canManualEntry: options.canManualEntry === true,
      canNoteOnly: options.canNoteOnly === true,
      canReviewMockEstimate: options.canReviewMockEstimate === true,
      canSaveConfirmed: false,
      requiresReview: options.requiresReview === true,
      estimateLabel: options.estimateLabel || null,
      disclaimer: MEAL_SNAPSHOT_ESTIMATE_DISCLAIMER
    });
  }

  function disabledState() {
    return uiState("disabled", {
      title: "Meal Snapshot Beta",
      message: "Meal Snapshot Beta is not available yet."
    });
  }

  function createInitialMealSnapshotBetaUiState(featureConfig) {
    if (!isEnabled(featureConfig)) return disabledState();
    return uiState("idle", {
      title: "Meal Snapshot Beta",
      message: "Take or upload a meal photo to create an AI-assisted estimate.",
      canManualEntry: true,
      canNoteOnly: true
    });
  }

  function createMealSnapshotPendingUiState(featureConfig) {
    if (!isEnabled(featureConfig)) return disabledState();
    return uiState("upload_pending", {
      title: "Building your starting point",
      message: "Meal Snapshot Beta is preparing an AI-assisted estimate. No meal record is created during this step.",
      canManualEntry: true,
      canNoteOnly: true
    });
  }

  function createMealSnapshotLocalFileReadyUiState(featureConfig) {
    if (!isEnabled(featureConfig)) return disabledState();
    return uiState("local_file_ready", {
      title: "Photo ready for Meal Snapshot Beta",
      message: "Testing mode: your photo was checked locally only. It was not analyzed or saved.",
      canManualEntry: true,
      canNoteOnly: true,
      canReviewMockEstimate: true
    });
  }

  function serviceFallbackActions(serviceResult) {
    const requested = Array.isArray(serviceResult?.manualFallback?.actions)
      ? serviceResult.manualFallback.actions
      : [];
    const allowed = new Set(["retry", "manual_entry", "save_note_only"]);
    const actions = new Set(requested.filter((action) => allowed.has(action)));

    if (serviceResult?.status === "provider_unavailable"
      || serviceResult?.status === "network_failure"
      || serviceResult?.status === "timeout"
      || serviceResult?.status === "rate_limited") {
      actions.add("retry");
      actions.add("manual_entry");
      actions.add("save_note_only");
    }
    return actions;
  }

  function retryState(serviceResult) {
    const actions = serviceFallbackActions(serviceResult);
    return uiState("retry_available", {
      title: "Meal Snapshot is temporarily unavailable",
      message: "Your photo was not saved. Try again, enter the meal manually, or add a note only.",
      canRetry: actions.has("retry"),
      canManualEntry: actions.has("manual_entry"),
      canNoteOnly: actions.has("save_note_only")
    });
  }

  function mapMealSnapshotResultToUiState(featureConfig, serviceResult) {
    if (!isEnabled(featureConfig)) return disabledState();

    switch (serviceResult?.status) {
      case "file_required":
        return uiState("no_file_selected", {
          title: "Choose a meal photo",
          message: "Select a JPEG, PNG, or WebP photo to continue.",
          canManualEntry: true,
          canNoteOnly: true
        });
      case "unsupported_media_type":
      case "invalid_file":
        return uiState("invalid_file", {
          title: "Choose a different photo",
          message: "Meal Snapshot Beta accepts JPEG, PNG, or WebP images.",
          canManualEntry: true,
          canNoteOnly: true
        });
      case "payload_too_large":
        return uiState("oversized_file", {
          title: "This photo is too large",
          message: "Choose a smaller photo, enter the meal manually, or add a note only.",
          canManualEntry: true,
          canNoteOnly: true
        });
      case "configuration_error":
      case "network_unavailable":
      case "auth_required":
      case "unauthorized":
        return uiState("upload_unavailable", {
          title: "Meal Snapshot upload is unavailable",
          message: "The Beta upload is not available right now. You can enter the meal manually or add a note only.",
          canManualEntry: true,
          canNoteOnly: true
        });
      case "provider_unavailable":
      case "network_failure":
      case "timeout":
      case "rate_limited":
      case "request_failed":
        return retryState(serviceResult);
      case "aborted":
        return uiState("idle", {
          title: "Meal Snapshot Beta",
          message: "The estimate was canceled. Nothing was saved.",
          canRetry: true,
          canManualEntry: true,
          canNoteOnly: true
        });
      case "review_needed":
        return uiState("draft_estimate", {
          title: "Estimated Meal Snapshot",
          message: "This AI-assisted estimate is a starting point. Review and adjust it before saving.",
          canRetry: true,
          canManualEntry: true,
          canNoteOnly: true,
          requiresReview: true,
          estimateLabel: "Estimate - review required"
        });
      default:
        return uiState("generic_error", {
          title: "Meal Snapshot could not continue",
          message: "Nothing was saved. Try again, enter the meal manually, or add a note only.",
          canRetry: true,
          canManualEntry: true,
          canNoteOnly: true
        });
    }
  }

  function selectMealSnapshotFallback(featureConfig, action) {
    if (!isEnabled(featureConfig)) return disabledState();
    if (action === "manual_entry") {
      return uiState("manual_entry", {
        title: "Enter your meal manually",
        message: "Add what you remember. A simple starting point is enough.",
        canManualEntry: true,
        canNoteOnly: true
      });
    }
    if (action === "save_note_only") {
      return uiState("note_only", {
        title: "Add a meal note",
        message: "You can add a simple note without a nutrition estimate.",
        canManualEntry: true,
        canNoteOnly: true
      });
    }
    if (action === "retry") {
      return uiState("retry_available", {
        title: "Try Meal Snapshot again",
        message: "Retry when you are ready. Nothing has been saved.",
        canRetry: true,
        canManualEntry: true,
        canNoteOnly: true
      });
    }
    return mapMealSnapshotResultToUiState(featureConfig, null);
  }

  function createMealSnapshotUiEvent(state) {
    return Object.freeze({
      event: "meal_snapshot_beta_ui_state",
      status: String(state?.status || "unknown").slice(0, 80),
      canRetry: state?.canRetry === true,
      canManualEntry: state?.canManualEntry === true,
      canNoteOnly: state?.canNoteOnly === true,
      canSaveConfirmed: false
    });
  }

  return Object.freeze({
    MEAL_SNAPSHOT_ESTIMATE_DISCLAIMER,
    createInitialMealSnapshotBetaUiState,
    createMealSnapshotLocalFileReadyUiState,
    createMealSnapshotPendingUiState,
    createMealSnapshotUiEvent,
    mapMealSnapshotResultToUiState,
    selectMealSnapshotFallback
  });
}));
