"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import * as React from "react";
import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@open-mercato/ui/primitives/dialog";
import { Button } from "@open-mercato/ui/primitives/button";
import { CrudForm } from "@open-mercato/ui/backend/CrudForm";
import { createCrud } from "@open-mercato/ui/backend/utils/crud";
import { apiCall } from "@open-mercato/ui/backend/utils/apiCall";
import { createCrudFormError } from "@open-mercato/ui/backend/utils/serverErrors";
import { flash } from "@open-mercato/ui/backend/FlashMessages";
import { useT } from "@open-mercato/shared/lib/i18n/context";
import { E } from "../../../../generated/entities.ids.generated.js";
import {
  buildProjectPayload,
  createProjectFormFields,
  createProjectFormGroups,
  createProjectFormSchema
} from "../../backend/staff/timesheets/projects/projectFormConfig.js";
function CreateProjectDialog({ open, onOpenChange, onProjectCreated }) {
  const t = useT();
  const formSchema = React.useMemo(() => createProjectFormSchema(), []);
  const fields = React.useMemo(() => createProjectFormFields(t), [t]);
  const groups = React.useMemo(() => createProjectFormGroups(t), [t]);
  return /* @__PURE__ */ jsx(Dialog, { open, onOpenChange, children: /* @__PURE__ */ jsxs(DialogContent, { className: "sm:max-w-lg", children: [
    /* @__PURE__ */ jsx(DialogHeader, { children: /* @__PURE__ */ jsx(DialogTitle, { children: t("staff.timesheets.projects.form.createTitle", "Create project") }) }),
    /* @__PURE__ */ jsx(
      CrudForm,
      {
        embedded: true,
        fields,
        groups,
        schema: formSchema,
        initialValues: {},
        entityIds: [E.staff.staff_time_project],
        submitLabel: t("staff.timesheets.projects.form.actions.create", "Create"),
        extraActions: /* @__PURE__ */ jsx(Button, { type: "button", variant: "ghost", onClick: () => onOpenChange(false), children: t("staff.timesheets.projects.form.actions.cancel", "Cancel") }),
        onSubmit: async (values) => {
          const payload = buildProjectPayload(values);
          const { result: created } = await createCrud(
            "staff/timesheets/time-projects",
            payload,
            { errorMessage: t("staff.timesheets.projects.errors.save", "Failed to save project.") }
          );
          const newId = created?.id;
          if (!newId) {
            throw createCrudFormError(
              t("staff.timesheets.projects.errors.save", "Failed to save project.")
            );
          }
          try {
            const selfRes = await apiCall("/api/staff/team-members/self");
            const staffMemberId = selfRes.result?.member?.id;
            if (staffMemberId) {
              await createCrud("staff/timesheets/time-projects/" + newId + "/employees", {
                staffMemberId,
                assignedStartDate: (/* @__PURE__ */ new Date()).toISOString().slice(0, 10),
                status: "active"
              }, { errorMessage: "" });
            }
          } catch {
          }
          onProjectCreated({
            id: newId,
            name: created?.name ?? values.name.trim(),
            code: created?.code ?? values.code?.trim() ?? null
          });
          flash(t("staff.timesheets.projects.messages.saved", "Project saved."), "success");
          onOpenChange(false);
        }
      }
    )
  ] }) });
}
export {
  CreateProjectDialog
};
//# sourceMappingURL=CreateProjectDialog.js.map
