'use strict';
import { c } from 'react/compiler-runtime';
import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
import { cn } from '../../../../fd-toolbox/lib/utils.js';
import { multiListItemTypes } from './multi-list-types.js';
import { getMultiListConfig } from './multi-list-config.js';
import { getMultilistResourceListConfig } from './MultiList.funcs.js';
import { useState, createElement } from 'react';
import { ResourceList } from '../../resource-list/ResourceList.js';
import { useAsyncEffect } from '../../../../fd-toolbox/hooks/use-async-effect.js';
import { fetchUiResourceMeta } from '../../ui-meta-providers.js';
import { resourceNames } from '../../../../fd-toolbox/resources/resource-names.js';
import { CommentList } from '../../comment/CommentList.js';
import MultiListEditResource from './MultiListEditResource.js';
import { ManyToOneTableHeaderCellTemplate } from './templates/ManyToOneTableHeaderCellTemplate.js';

var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", {
  value,
  configurable: true
});
function MultiList(t0) {
  const $ = c(2);
  const {
    multiListItems,
    className,
    onCustomAction,
    onMultiListResourceRelationChange,
    multiListConfig,
    commentList,
    onSavingStarting,
    onSavingEnding
  } = t0;
  const config = getMultiListConfig(multiListConfig.tableConfig, multiListConfig.customSections);
  const [uiResourceMeta, setUiResourceMeta] = useState();
  let t1;
  let t2;
  if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
    t1 = async () => {
      setUiResourceMeta(await fetchUiResourceMeta(resourceNames.comment));
    };
    t2 = [];
    $[0] = t1;
    $[1] = t2;
  } else {
    t1 = $[0];
    t2 = $[1];
  }
  useAsyncEffect(t1, t2);
  return jsxs("div", {
    className: cn("flex flex-col gap-6 pb-5 lg:pb-0", className),
    children: [multiListItems.map(multiListItem => {
      let content;
      if (multiListItem.type === multiListItemTypes.resource) {
        const resourceListConfig = getMultilistResourceListConfig(config, multiListItem, RenderManyToOneHeaderCell, onCustomAction, onMultiListResourceRelationChange);
        content = jsx("div", {
          id: multiListItem.relatedResourceElementId,
          className: "flex flex-col",
          children: jsx(ResourceList, {
            config: resourceListConfig
          })
        }, `${multiListItem.resourceId}_${multiListItem.relatedResourceElementId}`);
      } else {
        if (multiListItem.type === multiListItemTypes.editResource) {
          content = jsx(MultiListEditResource, {
            multiListItem,
            onSavingStarting,
            onSavingEnding
          }, `${multiListItem.resourceId}_${multiListItem.relatedResourceElementId}`);
        }
      }
      return content;
    }), jsx(MultiListCustomSections, {
      multiListConfig
    }), commentList && commentList.length > 0 && uiResourceMeta && jsxs(Fragment, {
      children: [jsx("h1", {
        className: "text-muted-foreground py-3 text-sm font-semibold",
        children: uiResourceMeta.resourceMeta.display.pluralName
      }), jsx("div", {
        className: "w-full px-0 lg:px-4",
        children: jsx(CommentList, {
          resources: commentList,
          resourceId: uiResourceMeta.resourceMeta.id,
          resourceName: resourceNames.comment
        })
      })]
    })]
  });
}
__name(MultiList, "MultiList");
function RenderManyToOneHeaderCell(props, multiListItem, onMultiListResourceRelationChange) {
  return /* @__PURE__ */jsx(ManyToOneTableHeaderCellTemplate, {
    parentResourcePropertyMeta: multiListItem.parentResourcePropertyMeta,
    resourceId: multiListItem.resourceId,
    multiListItem,
    onViewMultiListRelationChange: onMultiListResourceRelationChange,
    onCustomAction: props.onCustomAction
  });
}
__name(RenderManyToOneHeaderCell, "RenderManyToOneHeaderCell");
function MultiListCustomSections({
  multiListConfig
}) {
  const multiListCustomSections = multiListConfig.customSections;
  if (!multiListCustomSections) return void 0;
  const multiListPermittedSections = multiListCustomSections.filter(section => section.hasPermission);
  return multiListPermittedSections.map(permittedSection => createElement(permittedSection.component, {
    key: permittedSection.id,
    id: permittedSection.id
  }));
}
__name(MultiListCustomSections, "MultiListCustomSections");

export { MultiList, MultiList as default };
