"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import * as React from "react";
import { useT } from "@open-mercato/shared/lib/i18n/context";
import { Button } from "@open-mercato/ui/primitives/button";
import { apiCall } from "@open-mercato/ui/backend/utils/apiCall";
import { LeaveRequestPreview } from "./LeaveRequestPreview.js";
function LeaveRequestDetail(props) {
  const t = useT();
  const [executingActionId, setExecutingActionId] = React.useState(null);
  const [actionTakenByName, setActionTakenByName] = React.useState(null);
  const actionTakenAtLabel = formatDateTime(props.actionTakenAt);
  const hasActionTaken = Boolean(props.actionTaken);
  const actionTakenId = extractActionId(props.actionTaken);
  const actionTakenLabel = resolveActionLabel(actionTakenId, props.actions, t);
  const actionTakenByLabel = formatUserLabel(props.actionTakenByUserId, actionTakenByName, t);
  React.useEffect(() => {
    const userId = props.actionTakenByUserId;
    if (typeof userId !== "string" || userId.trim().length === 0) {
      setActionTakenByName(null);
      return;
    }
    let cancelled = false;
    const safeUserId = userId.trim();
    async function loadActionTakenByUserName() {
      const call = await apiCall(
        `/api/auth/users?id=${encodeURIComponent(safeUserId)}`
      );
      const entry = Array.isArray(call.result?.items) ? call.result.items[0] : null;
      const resolvedName = typeof entry?.name === "string" && entry.name.trim().length > 0 ? entry.name.trim() : typeof entry?.email === "string" && entry.email.trim().length > 0 ? entry.email.trim() : null;
      if (!cancelled) {
        setActionTakenByName(resolvedName);
      }
    }
    void loadActionTakenByUserName();
    return () => {
      cancelled = true;
    };
  }, [props.actionTakenByUserId]);
  return /* @__PURE__ */ jsxs("div", { className: "space-y-3 rounded border p-3", children: [
    /* @__PURE__ */ jsx(
      LeaveRequestPreview,
      {
        entityId: props.entityId,
        entityModule: props.entityModule,
        entityType: props.entityType,
        snapshot: props.snapshot,
        previewData: props.previewData,
        actionRequired: props.actionRequired,
        actionType: props.actionType,
        actionLabel: props.actionLabel
      }
    ),
    props.actions.length ? /* @__PURE__ */ jsx("div", { className: "flex flex-wrap gap-2", children: props.actions.map((action) => /* @__PURE__ */ jsx(
      Button,
      {
        type: "button",
        size: "sm",
        variant: action.variant ?? "default",
        disabled: executingActionId !== null || hasActionTaken,
        onClick: async () => {
          if (executingActionId !== null || hasActionTaken) return;
          setExecutingActionId(action.id);
          try {
            await props.onAction(action.id, { id: props.entityId });
          } finally {
            setExecutingActionId(null);
          }
        },
        children: executingActionId === action.id ? t("messages.actions.executing", "Executing...") : t(action.labelKey ?? action.id, action.id)
      },
      action.id
    )) }) : null,
    hasActionTaken ? /* @__PURE__ */ jsxs("p", { className: "text-xs text-muted-foreground", children: [
      t("messages.actions.taken", "Action taken"),
      ": ",
      actionTakenLabel,
      actionTakenAtLabel ? ` (${actionTakenAtLabel})` : "",
      actionTakenByLabel ? ` ${t("messages.actions.by", "by")} ${actionTakenByLabel}` : ""
    ] }) : null
  ] });
}
function formatDateTime(value) {
  if (!value) return "";
  const date = value instanceof Date ? value : new Date(value);
  if (Number.isNaN(date.getTime())) return "";
  return date.toLocaleString();
}
function extractActionId(actionTaken) {
  if (!actionTaken) return null;
  const value = actionTaken.trim();
  if (value.length === 0) return null;
  const parts = value.split(":");
  return parts[parts.length - 1] ?? value;
}
function resolveActionLabel(actionId, actions, t) {
  if (!actionId) return "-";
  const action = actions.find((item) => item.id === actionId);
  if (!action) return actionId;
  return t(action.labelKey ?? action.id, action.id);
}
function formatUserLabel(userId, userName, t) {
  if (!userId) return "";
  if (userName) return userName;
  return t("common.user", "user");
}
var LeaveRequestDetail_default = LeaveRequestDetail;
export {
  LeaveRequestDetail,
  LeaveRequestDetail_default as default
};
//# sourceMappingURL=LeaveRequestDetail.js.map
