import { jsx, jsxs } from 'react/jsx-runtime';
import * as React from 'react';
import { Button } from '../../../button.mjs';
import { Label } from '../../../label.mjs';
import { Switch } from '../../../switch.mjs';
import { Input } from '../../../input.mjs';
import { cn } from '../../../../lib/utils.mjs';

const LinkEditBlock = ({
  onSave,
  defaultIsNewTab,
  defaultUrl,
  defaultText,
  className
}) => {
  const formRef = React.useRef(null);
  const [url, setUrl] = React.useState(defaultUrl || "");
  const [text, setText] = React.useState(defaultText || "");
  const [isNewTab, setIsNewTab] = React.useState(defaultIsNewTab || false);
  const handleSave = React.useCallback(
    (e) => {
      e.preventDefault();
      if (formRef.current) {
        const isValid = Array.from(
          formRef.current.querySelectorAll("input")
        ).every((input) => input.checkValidity());
        if (isValid) {
          onSave(url, text, isNewTab);
        } else {
          formRef.current.querySelectorAll("input").forEach((input) => {
            if (!input.checkValidity()) {
              input.reportValidity();
            }
          });
        }
      }
    },
    [onSave, url, text, isNewTab]
  );
  return /* @__PURE__ */ jsx("div", { ref: formRef, children: /* @__PURE__ */ jsxs("div", { className: cn("space-y-4", className), children: [
    /* @__PURE__ */ jsxs("div", { className: "space-y-1", children: [
      /* @__PURE__ */ jsx(Label, { children: "URL" }),
      /* @__PURE__ */ jsx(
        Input,
        {
          type: "url",
          required: true,
          placeholder: "Enter URL",
          value: url,
          onChange: (e) => setUrl(e.target.value)
        }
      )
    ] }),
    /* @__PURE__ */ jsxs("div", { className: "space-y-1", children: [
      /* @__PURE__ */ jsx(Label, { children: "Display Text (optional)" }),
      /* @__PURE__ */ jsx(
        Input,
        {
          type: "text",
          placeholder: "Enter display text",
          value: text,
          onChange: (e) => setText(e.target.value)
        }
      )
    ] }),
    /* @__PURE__ */ jsxs("div", { className: "flex items-center space-x-2", children: [
      /* @__PURE__ */ jsx(Label, { children: "Open in New Tab" }),
      /* @__PURE__ */ jsx(Switch, { checked: isNewTab, onCheckedChange: setIsNewTab })
    ] }),
    /* @__PURE__ */ jsx("div", { className: "flex justify-end space-x-2", children: /* @__PURE__ */ jsx(Button, { type: "button", onClick: handleSave, children: "Save" }) })
  ] }) });
};
LinkEditBlock.displayName = "LinkEditBlock";

export { LinkEditBlock, LinkEditBlock as default };
