"use client";
'use strict';

const jsxRuntime = require('react/jsx-runtime');
const React = require('react');
const button = require('../../../../../../../ui/src/components/button.cjs');
const input = require('../../../../../../../ui/src/components/input.cjs');
const textarea = require('../../../../../../../ui/src/components/textarea.cjs');
const label = require('../../../../../../../ui/src/components/label.cjs');
const kanbanHooks = require('../../hooks/kanban-hooks.cjs');

function BoardForm({ board, onClose, onSuccess }) {
  const isEditing = !!board;
  const { createBoard, updateBoard, isCreating, isUpdating } = kanbanHooks.useBoardMutations();
  const [name, setName] = React.useState(board?.name || "");
  const [description, setDescription] = React.useState(board?.description || "");
  const [error, setError] = React.useState(null);
  const isPending = isCreating || isUpdating;
  const handleSubmit = async (e) => {
    e.preventDefault();
    setError(null);
    if (!name.trim()) {
      setError("Name is required");
      return;
    }
    try {
      if (isEditing && board) {
        await updateBoard(board.id, { name, description });
        onSuccess(board.id);
      } else {
        const newBoard = await createBoard({ name, description });
        if (newBoard?.id) {
          onSuccess(newBoard.id);
        }
      }
    } catch (err) {
      setError(err instanceof Error ? err.message : "An error occurred");
    }
  };
  return /* @__PURE__ */ jsxRuntime.jsxs("form", { onSubmit: handleSubmit, className: "space-y-4", children: [
    /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-2", children: [
      /* @__PURE__ */ jsxRuntime.jsx(label.Label, { htmlFor: "name", children: "Name *" }),
      /* @__PURE__ */ jsxRuntime.jsx(
        input.Input,
        {
          id: "name",
          value: name,
          onChange: (e) => setName(e.target.value),
          placeholder: "e.g., Project Alpha",
          disabled: isPending
        }
      )
    ] }),
    /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-2", children: [
      /* @__PURE__ */ jsxRuntime.jsx(label.Label, { htmlFor: "description", children: "Description" }),
      /* @__PURE__ */ jsxRuntime.jsx(
        textarea.Textarea,
        {
          id: "description",
          value: description,
          onChange: (e) => setDescription(e.target.value),
          placeholder: "Describe your board...",
          disabled: isPending,
          rows: 3
        }
      )
    ] }),
    error && /* @__PURE__ */ jsxRuntime.jsx("div", { className: "p-3 text-sm text-red-600 bg-red-50 border border-red-200 rounded-md", children: error }),
    /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex gap-2 pt-2", children: [
      /* @__PURE__ */ jsxRuntime.jsx(button.Button, { type: "submit", disabled: isPending, children: isPending ? isEditing ? "Updating..." : "Creating..." : isEditing ? "Update Board" : "Create Board" }),
      /* @__PURE__ */ jsxRuntime.jsx(
        button.Button,
        {
          type: "button",
          variant: "outline",
          onClick: onClose,
          disabled: isPending,
          children: "Cancel"
        }
      )
    ] })
  ] });
}

exports.BoardForm = BoardForm;
