function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
/*
 * Copyright 2026 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 React, { memo } from "react";
import { DatePicker, DateRangePicker, EMPTY_RANGE } from "../../shared/calendar/index.js";
import { FormField } from "../FormField.js";
import { CustomField } from "./CustomField.js";
import { DropdownField } from "./DropdownField.js";
import { FilePickerField } from "./FilePickerField.js";
import { NumberInputField } from "./NumberInputField.js";
import { ObjectSelectField } from "./ObjectSelectField.js";
import { ObjectSetField } from "./ObjectSetField.js";
import { RadioButtonsField } from "./RadioButtonsField.js";
import { SwitchField } from "./SwitchField.js";
import { TextAreaField } from "./TextAreaField.js";
import { TextInputField } from "./TextInputField.js";
const UNSUPPORTED_FIELD_MESSAGE = "Unsupported field type. Use a CUSTOM field instead";
export const FormFieldRenderer = /*#__PURE__*/memo(function ({
  fieldDefinition,
  value,
  onFieldValueChange,
  onBlur,
  onFieldBlur,
  isEdited,
  error,
  portalContainer
}) {
  const {
    label,
    isRequired,
    helperText,
    helperTextPlacement
  } = fieldDefinition;
  return /*#__PURE__*/React.createElement(FormField, {
    label: label,
    isRequired: isRequired,
    fieldKey: fieldDefinition.fieldKey,
    helperText: helperText,
    helperTextPlacement: helperTextPlacement,
    isEdited: isEdited,
    error: error,
    onBlur: onBlur
  }, renderFieldComponent(fieldDefinition, value, onFieldValueChange, error, portalContainer, onFieldBlur));
});
function renderFieldComponent(fieldDefinition, value, onChange, error, portalContainer, onFieldBlur) {
  const disabled = fieldDefinition.disabled === true;
  switch (fieldDefinition.fieldComponent) {
    case "DATE_RANGE_INPUT":
      return /*#__PURE__*/React.createElement(DateRangePicker, _extends({
        id: fieldDefinition.fieldKey,
        value: coerceToDateRange(value),
        onChange: onChange,
        placeholderStart: fieldDefinition.placeholder
      }, fieldDefinition.fieldComponentProps, {
        disabled: disabled,
        portalContainer: resolvePortalContainer(fieldDefinition.fieldComponentProps, portalContainer)
      }));
    case "TEXT_INPUT":
      return /*#__PURE__*/React.createElement(TextInputField, _extends({
        id: fieldDefinition.fieldKey,
        value: value != null ? String(value) : "",
        onChange: onChange,
        placeholder: fieldDefinition.placeholder,
        error: error
      }, fieldDefinition.fieldComponentProps, {
        disabled: disabled
      }));
    case "UNSUPPORTED":
      return /*#__PURE__*/React.createElement(TextInputField, _extends({}, fieldDefinition.fieldComponentProps, {
        id: fieldDefinition.fieldKey,
        value: UNSUPPORTED_FIELD_MESSAGE,
        error: error,
        disabled: true
      }));
    case "TEXT_AREA":
      return /*#__PURE__*/React.createElement(TextAreaField, _extends({
        id: fieldDefinition.fieldKey,
        value: value != null ? String(value) : "",
        onChange: onChange,
        placeholder: fieldDefinition.placeholder,
        error: error
      }, fieldDefinition.fieldComponentProps, {
        disabled: disabled
      }));
    case "DROPDOWN":
      {
        return /*#__PURE__*/React.createElement(DropdownField, _extends({
          id: fieldDefinition.fieldKey,
          value: value,
          onChange: onChange,
          placeholder: fieldDefinition.placeholder,
          error: error
        }, fieldDefinition.fieldComponentProps, {
          disabled: disabled,
          portalContainer: resolvePortalContainer(fieldDefinition.fieldComponentProps, portalContainer),
          onBlur: onFieldBlur
        }));
      }
    case "DATETIME_PICKER":
      return /*#__PURE__*/React.createElement(DatePicker, _extends({
        id: fieldDefinition.fieldKey,
        placeholder: fieldDefinition.placeholder
        // TODO: Use coerceFieldValue
        ,
        value: value instanceof Date ? value : null,
        onChange: onChange,
        error: error
      }, fieldDefinition.fieldComponentProps, {
        disabled: disabled,
        portalContainer: resolvePortalContainer(fieldDefinition.fieldComponentProps, portalContainer)
      }));
    case "RADIO_BUTTONS":
      return /*#__PURE__*/React.createElement(RadioButtonsField, _extends({
        id: fieldDefinition.fieldKey,
        value: value,
        onChange: onChange,
        error: error
      }, fieldDefinition.fieldComponentProps, {
        disabled: disabled
      }));
    case "SWITCH":
      return /*#__PURE__*/React.createElement(SwitchField, _extends({
        id: fieldDefinition.fieldKey,
        label: fieldDefinition.label,
        value: !!value,
        onChange: onChange,
        error: error
      }, fieldDefinition.fieldComponentProps, {
        disabled: disabled
      }));
    case "CUSTOM":
      return /*#__PURE__*/React.createElement(CustomField, _extends({
        id: fieldDefinition.fieldKey,
        value: value,
        onChange: onChange,
        error: error
      }, fieldDefinition.fieldComponentProps, {
        disabled: disabled
      }));
    case "NUMBER_INPUT":
      // TODO: Use coerceFieldValue
      return /*#__PURE__*/React.createElement(NumberInputField, _extends({
        id: fieldDefinition.fieldKey,
        value: typeof value === "number" ? value : null,
        onChange: onChange,
        placeholder: fieldDefinition.placeholder,
        error: error
      }, fieldDefinition.fieldComponentProps, {
        disabled: disabled
      }));
    case "FILE_PICKER":
      return /*#__PURE__*/React.createElement(FilePickerField, _extends({
        id: fieldDefinition.fieldKey,
        value: coerceToFileValue(value),
        onChange: onChange,
        error: error
      }, fieldDefinition.fieldComponentProps, {
        disabled: disabled
      }));
    case "OBJECT_SELECT":
      return /*#__PURE__*/React.createElement(ObjectSelectField, _extends({
        id: fieldDefinition.fieldKey,
        value: narrowToOsdkObject(value),
        onChange: onChange,
        placeholder: fieldDefinition.placeholder,
        error: error
      }, fieldDefinition.fieldComponentProps, {
        disabled: disabled,
        portalContainer: resolvePortalContainer(fieldDefinition.fieldComponentProps, portalContainer)
      }));
    case "OBJECT_SET":
      return /*#__PURE__*/React.createElement(ObjectSetField, _extends({
        id: fieldDefinition.fieldKey
      }, fieldDefinition.fieldComponentProps, {
        disabled: disabled
      }));
    default:
      return assertUnreachableFieldComponent(fieldDefinition);
  }
}
function resolvePortalContainer(fieldComponentProps, formPortalContainer) {
  // A field-level value, including null, is an explicit override. That lets a
  // single field opt out of a form-level dialog portal when needed.
  return Object.hasOwn(fieldComponentProps, "portalContainer") ? fieldComponentProps.portalContainer : formPortalContainer;
}
function coerceToDateRange(value) {
  if (!Array.isArray(value) || value.length !== 2) return EMPTY_RANGE;
  const start = value[0] instanceof Date ? value[0] : null;
  const end = value[1] instanceof Date ? value[1] : null;
  if (start == null && end == null) return EMPTY_RANGE;
  return [start, end];
}

// TODO: Move and share with `coerceFieldValue`
function isFileArray(value) {
  return value.every(v => v instanceof File);
}
function coerceToFileValue(value) {
  if (value instanceof File) {
    return value;
  }
  if (Array.isArray(value) && isFileArray(value)) {
    return value;
  }
  return null;
}

/** Narrows the untyped form value to an OsdkObject by checking for $primaryKey. */
function narrowToOsdkObject(value) {
  if (value != null && typeof value === "object" && "$primaryKey" in value) {
    return value;
  }
  return null;
}
function assertUnreachableFieldComponent(value) {
  throw new Error(`Unhandled field component: ${String(value)}`);
}
//# sourceMappingURL=FormFieldRenderer.js.map