// FeedbackPresenter.jsx — shared rendering for loading/error/success/empty states.

(function () {
  const STATE_DEFAULTS = {
    loading: {
      icon: "ri-loader-4-line",
      message: "Загрузка…",
      tone: "neutral",
      ariaBusy: true,
    },
    error: {
      icon: "ri-error-warning-line",
      message: "Не удалось выполнить действие",
      tone: "danger",
      ariaBusy: false,
    },
    warning: {
      icon: "ri-alert-line",
      message: "Требуется внимание",
      tone: "warning",
      ariaBusy: false,
    },
    info: {
      icon: "ri-information-line",
      message: "Информация",
      tone: "neutral",
      ariaBusy: false,
    },
    success: {
      icon: "ri-checkbox-circle-line",
      message: "Операция выполнена",
      tone: "success",
      ariaBusy: false,
    },
    empty: {
      icon: "ri-inbox-line",
      message: "Данных пока нет",
      tone: "muted",
      ariaBusy: false,
    },
  };

  function resolveState(state) {
    if (typeof state !== "string") return "empty";
    const normalized = state.trim().toLowerCase();
    return Object.prototype.hasOwnProperty.call(STATE_DEFAULTS, normalized)
      ? normalized
      : "empty";
  }

  function FeedbackPresenter(props) {
    const safeProps = props || {};
    const resolvedState = resolveState(safeProps.state);
    const defaults = STATE_DEFAULTS[resolvedState];

    const icon = safeProps.icon || defaults.icon;
    const message = safeProps.message || defaults.message;
    const detail = safeProps.detail || "";
    const title = safeProps.title || "";
    const tone = safeProps.tone || defaults.tone;
    const compact = !!safeProps.compact;
    const className = safeProps.className || "";
    const showIcon = safeProps.showIcon !== false;
    const ariaLive = safeProps.ariaLive || (resolvedState === "error" ? "assertive" : "polite");
    const id = typeof safeProps.id === "string" && safeProps.id ? safeProps.id : undefined;
    const onRetry = typeof safeProps.onRetry === "function" ? safeProps.onRetry : null;
    const retryLabel = typeof safeProps.retryLabel === "string" && safeProps.retryLabel
      ? safeProps.retryLabel
      : "Повторить";
    const action = safeProps.action || (
      onRetry
        ? (
            <button type="button" className="btn btn--secondary btn--sm" onClick={onRetry}>
              {retryLabel}
            </button>
          )
        : null
    );
    const detailId = detail && id ? `${id}__detail` : undefined;

    const classes = ["feedback-presenter", `feedback-presenter--${resolvedState}`, `feedback-presenter--${tone}`];
    if (compact) classes.push("feedback-presenter--compact");
    if (className) classes.push(className);

    return (
      <div
        className={classes.join(" ")}
        id={id}
        role={resolvedState === "error" ? "alert" : "status"}
        aria-live={ariaLive}
        aria-busy={defaults.ariaBusy ? "true" : undefined}
        aria-atomic={resolvedState === "error" ? "true" : "false"}
        aria-describedby={detailId}
      >
        {showIcon && (
          <span className="feedback-presenter__icon" aria-hidden="true">
            <i className={icon}></i>
          </span>
        )}

        <div className="feedback-presenter__body">
          {title && <div className="feedback-presenter__title">{title}</div>}
          <div className="feedback-presenter__message">{message}</div>
          {detail && <div id={detailId} className="feedback-presenter__detail">{detail}</div>}
        </div>

        {action && <div className="feedback-presenter__action">{action}</div>}
      </div>
    );
  }

  window.FeedbackPresenter = FeedbackPresenter;
})();
