'use strict';
'use client';
import { c } from 'react/compiler-runtime';
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
import { useState, useEffect } from 'react';
import { crudActions, propertyTypes } from '../../../../fd-toolbox/enums/enums.js';
import { Markdown } from '../../../fd-component-toolbox/markdown/Markdown.js';
import { Switch } from '../../../fd-component-toolbox/switch/Switch.js';
import { EditResourceEnumField } from '../edit-resource-property-block/EditResourceEnumField.js';
import { EditResourceFlagsEnumField } from '../edit-resource-property-block/EditResourceFlagsEnumField.js';
import { Input } from '../../../fd-component-toolbox/input/Input.js';
import { DatePicker } from '../../../fd-component-toolbox/datepicker/DatePicker.js';
import { Textarea } from '../../../fd-component-toolbox/textarea/TextArea.js';
import { FormField, FormItem, FormControl, FormMessage } from '../../../fd-component-toolbox/form/Form.js';
import { isBlobList, isMarkdown, isEnum, isFlagsEnum, isRichText, isDateOnly, isDateTime, isBigText } from '../../core/property-types.js';
import { getRelationResourceId, onMultiBrowserChange, onBrowserChange, isRelationField, getRequiredLabel, isManyToOneBrowserField, isOneToManyBrowserField, isManyToManyBrowserField, onEditorChange, isIdProperty } from '../../services/property-fields.js';
import { ensureDateStringInIso } from '../../../../fd-toolbox/dates/date-time.js';
import { fdBrowserField, fdMultiBrowserField, fdManyToManyBrowserField } from '../edit-resource-property-block/edit-resource-fields.js';
import { isImageField, isDefaultInputField } from '../services/edit-resource-components.js';
import { EditResourceBlobField } from '../edit-resource-property-block/EditResourceBlobField.js';
import { EditResourceRichTextField } from '../edit-resource-property-block/EditResourceRichTextField.js';
import { cn } from '../../../../fd-toolbox/lib/utils.js';
import { hasPropertyPermission } from '../../auth/permissions/permission-checker.js';
import { EditResourceImageField } from '../edit-resource-property-block/EditResourceImageField.js';
import { syncBooleanField, getEditResourcePropertyFieldId, onCheckedChange } from './EditResourcePropertyField.funcs.js';

var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", {
  value,
  configurable: true
});
function EditResourcePropertyField(t0) {
  const $ = c(15);
  const {
    uiPropertyMeta,
    propertyMeta,
    form,
    formFieldOption,
    resource,
    propertyConfig,
    readOnlyResource,
    loadComplete,
    excludeResourceIds,
    isNew,
    onChange,
    onItemClick,
    parentContextName
  } = t0;
  let result;
  const displayName = propertyMeta.displayName;
  const defaultValue = form.getValues(propertyMeta.name);
  const relatedResourceId = getRelationResourceId(propertyMeta, resource);
  const hasPermission = hasPropertyPermission(propertyMeta.id, crudActions.update);
  const [editorContent, setEditorContent] = useState("");
  const [isChecked, setIsChecked] = useState(!!resource[uiPropertyMeta.name]);
  let t1;
  let t2;
  if ($[0] !== form || $[1] !== propertyMeta || $[2] !== resource || $[3] !== uiPropertyMeta) {
    t1 = () => {
      syncBooleanField(propertyMeta, uiPropertyMeta, form, resource);
    };
    t2 = [form, propertyMeta, resource, uiPropertyMeta];
    $[0] = form;
    $[1] = propertyMeta;
    $[2] = resource;
    $[3] = uiPropertyMeta;
    $[4] = t1;
    $[5] = t2;
  } else {
    t1 = $[4];
    t2 = $[5];
  }
  useEffect(t1, t2);
  let t3;
  if ($[6] !== form || $[7] !== onChange) {
    t3 = name => {
      onChange?.(form.getValues(name), name);
    };
    $[6] = form;
    $[7] = onChange;
    $[8] = t3;
  } else {
    t3 = $[8];
  }
  const onTextFieldChange = __name(t3, "onTextFieldChange");
  let t4;
  if ($[9] !== form.setValue || $[10] !== onChange) {
    t4 = (selected, propertyMetaValue) => {
      onMultiBrowserChange(form.setValue, selected, propertyMetaValue, onChange);
    };
    $[9] = form.setValue;
    $[10] = onChange;
    $[11] = t4;
  } else {
    t4 = $[11];
  }
  const onMultiBrowserValueChange = __name(t4, "onMultiBrowserValueChange");
  let t5;
  if ($[12] !== form.setValue || $[13] !== onChange) {
    t5 = (propertyMetaValue_0, value) => {
      onBrowserChange(form.setValue, value, propertyMetaValue_0, onChange);
    };
    $[12] = form.setValue;
    $[13] = onChange;
    $[14] = t5;
  } else {
    t5 = $[14];
  }
  const onBrowserValueChange = __name(t5, "onBrowserValueChange");
  if (!hasPermission) {
    return result;
  }
  if (isRelationField(propertyMeta)) {
    return isBlobList(propertyMeta) ? jsx(EditResourceBlobField, {
      isNew,
      label: getRequiredLabel(propertyMeta.required, displayName),
      form,
      relatedResourceId: resource.id,
      propertyMeta
    }) : jsxs(Fragment, {
      children: [isManyToOneBrowserField(propertyMeta) && fdBrowserField(uiPropertyMeta, readOnlyResource, onBrowserValueChange, excludeResourceIds, relatedResourceId, form, onItemClick, isNew, propertyMeta, parentContextName), isOneToManyBrowserField(propertyMeta) && fdMultiBrowserField(readOnlyResource, onMultiBrowserValueChange, resource.id, form, onItemClick, isNew, propertyMeta, parentContextName), isManyToManyBrowserField(propertyMeta) && fdManyToManyBrowserField(readOnlyResource, relatedResourceId, form, onItemClick, isNew, propertyMeta, onMultiBrowserValueChange, parentContextName)]
    });
  } else {
    return jsxs(Fragment, {
      children: [isMarkdown(propertyMeta.type, propertyMeta.typeFormat) && jsx(FormField, {
        control: form.control,
        name: uiPropertyMeta.name,
        render: t6 => {
          const {
            field,
            fieldState
          } = t6;
          return jsxs(FormItem, {
            children: [jsx(FormControl, {
              children: jsx(Markdown, {
                id: getEditResourcePropertyFieldId(uiPropertyMeta.name, parentContextName),
                label: getRequiredLabel(propertyMeta.required, displayName),
                required: propertyMeta.required,
                value: field.value,
                readOnly: readOnlyResource,
                skipAnimationOnce: !loadComplete,
                onChange: field.onChange,
                onBlur: () => onTextFieldChange(propertyMeta.name),
                error: !!fieldState.error
              })
            }), jsx(FormMessage, {})]
          });
        }
      }), propertyMeta.type === propertyTypes.boolean && jsx(FormField, {
        control: form.control,
        name: uiPropertyMeta.name,
        render: t7 => {
          const {
            field: field_0
          } = t7;
          return jsxs(FormItem, {
            children: [jsx(FormControl, {
              children: jsx("div", {
                id: getEditResourcePropertyFieldId(uiPropertyMeta.name, parentContextName),
                children: jsx(Switch, {
                  label: displayName,
                  checked: isChecked,
                  disabled: propertyConfig?.disabled ?? false,
                  readOnly: readOnlyResource,
                  onCheckedChange: checked => onCheckedChange(checked, field_0.onChange, setIsChecked, onTextFieldChange, uiPropertyMeta.name)
                })
              })
            }), jsx(FormMessage, {})]
          });
        }
      }), isEnum(propertyMeta.type, propertyMeta.typeFormat) && jsx(EditResourceEnumField, {
        uiPropertyMeta,
        form,
        readOnly: readOnlyResource,
        skipAnimationOnce: !loadComplete,
        onChange,
        propertyMeta,
        parentContextName: getEditResourcePropertyFieldId(uiPropertyMeta.name, parentContextName)
      }), isFlagsEnum(propertyMeta.type, propertyMeta.typeFormat) && jsx(EditResourceFlagsEnumField, {
        uiPropertyMeta,
        form,
        skipAnimationOnce: !loadComplete,
        readOnly: readOnlyResource,
        onChange,
        propertyMeta,
        parentContextName: getEditResourcePropertyFieldId(uiPropertyMeta.name, parentContextName)
      }), isRichText(propertyMeta.type, propertyMeta.typeFormat) && jsx(FormItem, {
        children: jsx(FormField, {
          control: form.control,
          name: uiPropertyMeta.name,
          render: () => jsx(EditResourceRichTextField, {
            id: getEditResourcePropertyFieldId(uiPropertyMeta.name, parentContextName),
            value: editorContent,
            onChange: content => onEditorChange(form, propertyMeta.name, content, setEditorContent),
            placeholder: "Start typing...",
            debounceTime: 500,
            defaultValue: defaultValue ? defaultValue : "",
            label: getRequiredLabel(propertyMeta.required, displayName),
            onBlur: () => onTextFieldChange(propertyMeta.name)
          })
        })
      }), isImageField(propertyMeta) && jsx(EditResourceImageField, {
        isNew,
        form,
        propertyMeta,
        relatedResourceId,
        onChange
      }), isDefaultInputField(propertyMeta) && jsxs("div", {
        className: cn("w-full", isIdProperty(propertyMeta) && "opacity-0"),
        children: [jsx(FormField, {
          control: form.control,
          name: uiPropertyMeta.name,
          render: t8 => {
            const {
              field: field_1
            } = t8;
            return jsxs(FormItem, {
              children: [jsx(FormControl, {
                children: jsx(Input, {
                  style: {
                    cursor: isIdProperty(propertyMeta) ? "default" : "text"
                  },
                  showClearIcon: !readOnlyResource,
                  ...form.register(propertyMeta.name, formFieldOption),
                  ...field_1,
                  readOnly: readOnlyResource,
                  placeholder: displayName,
                  inputId: getEditResourcePropertyFieldId(uiPropertyMeta.name, parentContextName),
                  required: propertyMeta.required,
                  label: getRequiredLabel(propertyMeta.required, displayName),
                  onBlur: () => onTextFieldChange(propertyMeta.name),
                  skipAnimationOnce: !loadComplete,
                  onChange: field_1.onChange,
                  disabled: propertyConfig?.disabled ?? false
                })
              }), jsx(FormMessage, {})]
            });
          }
        }), propertyConfig?.description && jsx("p", {
          className: "mt-2 pl-1 text-xs opacity-60",
          children: propertyConfig.description
        })]
      }), (isDateOnly(propertyMeta.type, propertyMeta.typeFormat) || isDateTime(propertyMeta.type, propertyMeta.typeFormat)) && jsx(FormField, {
        control: form.control,
        name: propertyMeta.name,
        render: t9 => {
          const {
            field: field_2
          } = t9;
          return jsxs(FormItem, {
            children: [jsx(FormControl, {
              children: jsx(DatePicker, {
                label: displayName,
                date: field_2.value,
                inputId: getEditResourcePropertyFieldId(uiPropertyMeta.name, parentContextName),
                enableTime: isDateTime(propertyMeta.type, propertyMeta.typeFormat),
                readOnly: readOnlyResource,
                skipAnimationOnce: !loadComplete,
                onDateValueChange: val => val && onChange?.(ensureDateStringInIso(val), uiPropertyMeta.name)
              })
            }), jsx(FormMessage, {})]
          });
        }
      }), isBigText(propertyMeta.type, propertyMeta.typeFormat) && jsx("div", {
        className: "w-full",
        children: jsx(FormField, {
          control: form.control,
          name: uiPropertyMeta.name,
          render: t10 => {
            const {
              field: field_3
            } = t10;
            return jsxs(FormItem, {
              children: [jsx(FormControl, {
                children: jsx(Textarea, {
                  placeholder: propertyConfig?.placeholder,
                  required: propertyMeta.required,
                  id: getEditResourcePropertyFieldId(uiPropertyMeta.name, parentContextName),
                  ...field_3,
                  ...form.register(propertyMeta.name, formFieldOption),
                  readOnly: readOnlyResource,
                  onChange: field_3.onChange,
                  label: getRequiredLabel(propertyMeta.required, displayName),
                  onBlur: () => onTextFieldChange(propertyMeta.name)
                })
              }), jsx(FormMessage, {})]
            });
          }
        })
      })]
    });
  }
}
__name(EditResourcePropertyField, "EditResourcePropertyField");
EditResourcePropertyField.displayName = "EditResourcePropertyField";

export { EditResourcePropertyField };
