'use strict';
'use client';
import { c } from 'react/compiler-runtime';
import { jsx } from 'react/jsx-runtime';
import { useState } from 'react';
import { writeValue, closeManyToManyBrowserDialog, onManyToManyBrowserInputUpdate, getManyToManyBrowserRelatedResourcePropertyMeta } from './ManyToManyBrowser.funcs.js';
import { useAsyncEffect } from '../../../../fd-toolbox/hooks/use-async-effect.js';
import { BaseMultiBrowser } from '../base-multi-browser/BaseMultiBrowser.js';
import { browserTypes } from '../browser-type.js';

var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", {
  value,
  configurable: true
});
function ManyToManyBrowser(t0) {
  const $ = c(32);
  const {
    required,
    propertyMeta,
    onChange,
    parentContextName: parentContextNameProp,
    displayMode,
    disabled,
    readOnly,
    relatedResourceId,
    invalid,
    onSelectedResourceIdsChange,
    children: icons
  } = t0;
  const [propertyResourceMeta, setPropertyResourceMeta] = useState();
  let t1;
  if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
    t1 = [];
    $[0] = t1;
  } else {
    t1 = $[0];
  }
  const [selectedResources, setSelectedResources] = useState(t1);
  const [relatedResourcePropertyMeta, setRelatedResourcePropertyMeta] = useState();
  const [loadComplete, setLoadComplete] = useState(false);
  let t2;
  if ($[1] !== onSelectedResourceIdsChange || $[2] !== propertyMeta || $[3] !== propertyResourceMeta || $[4] !== relatedResourceId) {
    t2 = async () => {
      await writeValue(propertyMeta, setSelectedResources, setPropertyResourceMeta, setLoadComplete, setRelatedResourcePropertyMeta, propertyResourceMeta, relatedResourceId, onSelectedResourceIdsChange);
    };
    $[1] = onSelectedResourceIdsChange;
    $[2] = propertyMeta;
    $[3] = propertyResourceMeta;
    $[4] = relatedResourceId;
    $[5] = t2;
  } else {
    t2 = $[5];
  }
  let t3;
  if ($[6] !== propertyMeta.name) {
    t3 = [propertyMeta.name];
    $[6] = propertyMeta.name;
    $[7] = t3;
  } else {
    t3 = $[7];
  }
  useAsyncEffect(t2, t3);
  let t4;
  if ($[8] !== disabled || $[9] !== displayMode || $[10] !== icons || $[11] !== invalid || $[12] !== loadComplete || $[13] !== onChange || $[14] !== parentContextNameProp || $[15] !== propertyMeta || $[16] !== propertyResourceMeta || $[17] !== readOnly || $[18] !== relatedResourceId || $[19] !== relatedResourcePropertyMeta || $[20] !== required || $[21] !== selectedResources) {
    let t5;
    if ($[23] !== onChange || $[24] !== propertyMeta || $[25] !== propertyResourceMeta) {
      t5 = resources => closeManyToManyBrowserDialog(resources, setSelectedResources, propertyMeta, setPropertyResourceMeta, propertyResourceMeta, onChange);
      $[23] = onChange;
      $[24] = propertyMeta;
      $[25] = propertyResourceMeta;
      $[26] = t5;
    } else {
      t5 = $[26];
    }
    const onCloseDialog = __name(t5, "onCloseDialog");
    let t6;
    if ($[27] !== onChange || $[28] !== propertyResourceMeta) {
      t6 = e => onManyToManyBrowserInputUpdate(e.target.value, setSelectedResources, propertyResourceMeta, onChange);
      $[27] = onChange;
      $[28] = propertyResourceMeta;
      $[29] = t6;
    } else {
      t6 = $[29];
    }
    let t7;
    if ($[30] !== propertyMeta) {
      t7 = async () => setRelatedResourcePropertyMeta(await getManyToManyBrowserRelatedResourcePropertyMeta(propertyMeta));
      $[30] = propertyMeta;
      $[31] = t7;
    } else {
      t7 = $[31];
    }
    t4 = jsx(BaseMultiBrowser, {
      parentContextName: parentContextNameProp,
      browserType: browserTypes.manyToManyBrowser,
      required,
      propertyMeta,
      relatedResourceId,
      selectedResources,
      onCloseDialog,
      onDescriptionChange: t6,
      displayMode,
      disabled,
      readOnly,
      relatedResourcePropertyMeta,
      setSelectedResources,
      onSetRelatedResourcePropertyMeta: t7,
      propertyResourceMeta,
      skipAnimationOnce: !loadComplete,
      invalid,
      children: icons
    });
    $[8] = disabled;
    $[9] = displayMode;
    $[10] = icons;
    $[11] = invalid;
    $[12] = loadComplete;
    $[13] = onChange;
    $[14] = parentContextNameProp;
    $[15] = propertyMeta;
    $[16] = propertyResourceMeta;
    $[17] = readOnly;
    $[18] = relatedResourceId;
    $[19] = relatedResourcePropertyMeta;
    $[20] = required;
    $[21] = selectedResources;
    $[22] = t4;
  } else {
    t4 = $[22];
  }
  return t4;
}
__name(ManyToManyBrowser, "ManyToManyBrowser");

export { ManyToManyBrowser };
