'use strict';
'use client';
import { jsx } from 'react/jsx-runtime';
import { Icon, iconNames } from '../../../fd-component-toolbox/icon/Icon.js';
import { Browser } from '../../browsers/browser/Browser.js';
import { ManyToManyBrowser } from '../../browsers/many-to-many-browser/ManyToManyBrowser.js';
import { MultiBrowser } from '../../browsers/multi-browser/MultiBrowser.js';
import { FormField } from '../../../fd-component-toolbox/form/Form.js';
import { Button } from '../../../fd-component-toolbox/button/Button.js';
import { isManyToOneBrowserField, isOneToManyBrowserField, isManyToManyBrowserField } from '../../services/property-fields.js';

var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", {
  value,
  configurable: true
});
function fdBrowserFields(resource, uiPropertyMeta, readOnlyResource, onBrowserValueChange, excludeResourceIds, relatedResourceId, form, onItemClick, isNew, propertyMeta, onMultiBrowserValueChange, parentContextName) {
  let result;
  if (isManyToOneBrowserField(propertyMeta)) {
    result = fdBrowserField(uiPropertyMeta, readOnlyResource, onBrowserValueChange, excludeResourceIds, relatedResourceId, form, onItemClick, isNew, propertyMeta, parentContextName);
  } else if (isOneToManyBrowserField(propertyMeta)) {
    result = fdMultiBrowserField(readOnlyResource, onMultiBrowserValueChange, resource.id, form, onItemClick, isNew, propertyMeta, parentContextName);
  } else if (isManyToManyBrowserField(propertyMeta)) {
    result = fdManyToManyBrowserField(readOnlyResource, relatedResourceId, form, onItemClick, isNew, propertyMeta, onMultiBrowserValueChange, parentContextName);
    return result;
  }
  return result;
}
__name(fdBrowserFields, "fdBrowserFields");
function fdBrowserField(uiPropertyMeta, readOnlyResource, onBrowserValueChange, excludeResourceIds, relatedResourceId, form, onItemClick, isNew, propertyMeta, parentContextName) {
  return /* @__PURE__ */jsx("div", {
    className: "relative flex items-center",
    children: /* @__PURE__ */jsx(FormField, {
      control: form.control,
      name: propertyMeta.name,
      render: ({
        field,
        fieldState
      }) => /* @__PURE__ */jsx(Browser, {
        required: propertyMeta.required,
        readOnly: readOnlyResource,
        onChange: val => onBrowserValueChange(propertyMeta, val),
        propertyMeta,
        parentContextName,
        excludeResourceIds: excludeResourceIds.get(uiPropertyMeta.name),
        relatedResourceId,
        value: field.value ?? "",
        invalid: !!fieldState.error,
        children: !isNew && /* @__PURE__ */jsx(Button, {
          type: "button",
          variant: "icon",
          size: "icon",
          className: "size-4 cursor-pointer p-0",
          onClick: () => onItemClick(propertyMeta),
          children: /* @__PURE__ */jsx(Icon, {
            name: iconNames.link
          })
        })
      })
    })
  });
}
__name(fdBrowserField, "fdBrowserField");
function fdMultiBrowserField(readOnlyResource, onMultiBrowserValueChange, relatedResourceId, form, onItemClick, isNew, propertyMeta, parentContextName) {
  return /* @__PURE__ */jsx("div", {
    className: "relative flex items-center",
    children: /* @__PURE__ */jsx(FormField, {
      control: form.control,
      name: propertyMeta.name,
      render: ({
        fieldState
      }) => /* @__PURE__ */jsx(MultiBrowser, {
        parentContextName,
        relatedResourceId,
        propertyMeta,
        required: propertyMeta.required,
        readOnly: readOnlyResource,
        onChange: selected => onMultiBrowserValueChange(selected, propertyMeta),
        onSelectedResourceIdsChange: ids => form.setValue(propertyMeta.name, ids.join(",")),
        invalid: !!fieldState.error,
        variant: "float",
        children: !isNew && /* @__PURE__ */jsx(Button, {
          type: "button",
          variant: "icon",
          size: "icon",
          className: "size-4 cursor-pointer p-0",
          onClick: () => onItemClick(propertyMeta),
          children: /* @__PURE__ */jsx(Icon, {
            name: iconNames.link
          })
        })
      })
    })
  });
}
__name(fdMultiBrowserField, "fdMultiBrowserField");
function fdManyToManyBrowserField(readOnlyResource, relatedResourceId, form, onItemClick, isNew, propertyMeta, onMultiBrowserValueChange, parentContextName) {
  return /* @__PURE__ */jsx("div", {
    className: "relative flex items-center",
    children: /* @__PURE__ */jsx(FormField, {
      control: form.control,
      name: propertyMeta.name,
      render: ({
        fieldState
      }) => /* @__PURE__ */jsx(ManyToManyBrowser, {
        parentContextName,
        relatedResourceId,
        propertyMeta,
        required: propertyMeta.required,
        onChange: selected => onMultiBrowserValueChange(selected, propertyMeta),
        onSelectedResourceIdsChange: ids => form.setValue(propertyMeta.name, ids.join(",")),
        invalid: !!fieldState.error,
        readOnly: readOnlyResource,
        children: !isNew && /* @__PURE__ */jsx(Button, {
          type: "button",
          variant: "icon",
          size: "icon",
          className: "size-4 cursor-pointer p-0",
          onClick: () => onItemClick(propertyMeta),
          children: /* @__PURE__ */jsx(Icon, {
            name: iconNames.link
          })
        })
      })
    })
  });
}
__name(fdManyToManyBrowserField, "fdManyToManyBrowserField");

export { fdBrowserField, fdBrowserFields, fdManyToManyBrowserField, fdMultiBrowserField };
