/*
 * Copyright 2025 Palantir Technologies, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { Popover } from "@base-ui/react/popover";
import { InfoSign } from "@blueprintjs/icons";
import React, { memo } from "react";
import styles from "./FormField.module.css";
export const FormField = /*#__PURE__*/memo(function ({
  fieldKey,
  label,
  isRequired,
  helperText,
  helperTextPlacement = "tooltip",
  isEdited,
  error,
  onBlur,
  children
}) {
  const hasHelperText = helperText != null && helperText !== "";
  const showTooltip = hasHelperText && helperTextPlacement === "tooltip";
  const showEditedTag = isEdited === true;
  const labelElement = label != null ? /*#__PURE__*/React.createElement("label", {
    className: styles.osdkFormFieldLabel,
    htmlFor: fieldKey
  }, label, isRequired === true && /*#__PURE__*/React.createElement("span", {
    className: styles.osdkFormFieldRequired,
    "aria-label": "required"
  }, " ", "*")) : null;
  const labelRow = labelElement != null || showTooltip || showEditedTag ? /*#__PURE__*/React.createElement("div", {
    className: styles.osdkFormFieldLabelRow
  }, labelElement, showTooltip && /*#__PURE__*/React.createElement(InfoTip, {
    label: label
  }, helperText), showEditedTag && /*#__PURE__*/React.createElement("span", {
    className: styles.osdkFormFieldEditedTag
  }, "Edited")) : null;
  return /*#__PURE__*/React.createElement("div", {
    className: styles.osdkFormField,
    onBlur: onBlur
  }, labelRow, hasHelperText && helperTextPlacement === "bottom" && /*#__PURE__*/React.createElement("div", {
    className: styles.osdkFormFieldHelperText
  }, helperText), children, /*#__PURE__*/React.createElement("div", {
    className: styles.osdkFormFieldErrorSlot,
    "data-osdk-form-field-error-slot": ""
  }, error != null && /*#__PURE__*/React.createElement("div", {
    className: styles.osdkFormFieldError,
    role: "alert"
  }, error)));
});
// Uses Popover (not Tooltip) because helper text may contain interactive
// content like links that need focus management and keyboard navigation.
// See https://base-ui.com/react/components/tooltip#infotips
function InfoTip({
  label,
  children
}) {
  return /*#__PURE__*/React.createElement(Popover.Root, null, /*#__PURE__*/React.createElement(Popover.Trigger, {
    render: /*#__PURE__*/React.createElement("span", {
      className: styles.osdkFormFieldInfoIcon
    }),
    nativeButton: false,
    openOnHover: true,
    "aria-label": label != null ? `Info about ${label}` : "More information"
  }, /*#__PURE__*/React.createElement(InfoSign, {
    size: 12
  })), /*#__PURE__*/React.createElement(Popover.Portal, null, /*#__PURE__*/React.createElement(Popover.Positioner, {
    sideOffset: 4
  }, /*#__PURE__*/React.createElement(Popover.Popup, {
    className: styles.osdkFormFieldInfoPopup
  }, children))));
}
//# sourceMappingURL=FormField.js.map