'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const jsxRuntime = require('react/jsx-runtime');
const React = require('react');
const button = require('../../../button.cjs');
const label = require('../../../label.cjs');
const _switch = require('../../../switch.cjs');
const input = require('../../../input.cjs');
const utils = require('../../../../lib/utils.cjs');

function _interopNamespaceCompat(e) {
  if (e && typeof e === 'object' && 'default' in e) return e;
  const n = Object.create(null);
  if (e) {
    for (const k in e) {
      n[k] = e[k];
    }
  }
  n.default = e;
  return n;
}

const React__namespace = /*#__PURE__*/_interopNamespaceCompat(React);

const LinkEditBlock = ({
  onSave,
  defaultIsNewTab,
  defaultUrl,
  defaultText,
  className
}) => {
  const formRef = React__namespace.useRef(null);
  const [url, setUrl] = React__namespace.useState(defaultUrl || "");
  const [text, setText] = React__namespace.useState(defaultText || "");
  const [isNewTab, setIsNewTab] = React__namespace.useState(defaultIsNewTab || false);
  const handleSave = React__namespace.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__ */ jsxRuntime.jsx("div", { ref: formRef, children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: utils.cn("space-y-4", className), children: [
    /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-1", children: [
      /* @__PURE__ */ jsxRuntime.jsx(label.Label, { children: "URL" }),
      /* @__PURE__ */ jsxRuntime.jsx(
        input.Input,
        {
          type: "url",
          required: true,
          placeholder: "Enter URL",
          value: url,
          onChange: (e) => setUrl(e.target.value)
        }
      )
    ] }),
    /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-1", children: [
      /* @__PURE__ */ jsxRuntime.jsx(label.Label, { children: "Display Text (optional)" }),
      /* @__PURE__ */ jsxRuntime.jsx(
        input.Input,
        {
          type: "text",
          placeholder: "Enter display text",
          value: text,
          onChange: (e) => setText(e.target.value)
        }
      )
    ] }),
    /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center space-x-2", children: [
      /* @__PURE__ */ jsxRuntime.jsx(label.Label, { children: "Open in New Tab" }),
      /* @__PURE__ */ jsxRuntime.jsx(_switch.Switch, { checked: isNewTab, onCheckedChange: setIsNewTab })
    ] }),
    /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex justify-end space-x-2", children: /* @__PURE__ */ jsxRuntime.jsx(button.Button, { type: "button", onClick: handleSave, children: "Save" }) })
  ] }) });
};
LinkEditBlock.displayName = "LinkEditBlock";

exports.LinkEditBlock = LinkEditBlock;
exports.default = LinkEditBlock;
