/**
 * This file is part of the NocoBase (R) project.
 * Copyright (c) 2020-2024 NocoBase Co., Ltd.
 * Authors: NocoBase Team.
 *
 * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
 * For more information, please refer to: https://www.nocobase.com/agreement.
 */

var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var hidden_popup_calendar_exports = {};
__export(hidden_popup_calendar_exports, {
  CALENDAR_POPUP_ACTION_KEYS: () => CALENDAR_POPUP_ACTION_KEYS,
  CALENDAR_POPUP_PROP_KEYS: () => CALENDAR_POPUP_PROP_KEYS,
  buildCalendarInitialStepParams: () => buildCalendarInitialStepParams,
  buildCalendarPopupOpenView: () => buildCalendarPopupOpenView,
  ensureCalendarBlockPopupHosts: () => ensureCalendarBlockPopupHosts,
  getCalendarBlockResourceInit: () => getCalendarBlockResourceInit,
  getCalendarPopupActionUid: () => getCalendarPopupActionUid,
  getCalendarPopupActionUse: () => getCalendarPopupActionUse,
  getCalendarPopupPropKey: () => getCalendarPopupPropKey,
  getCalendarPopupStoredSettings: () => getCalendarPopupStoredSettings,
  mergeCalendarPopupSettings: () => mergeCalendarPopupSettings,
  normalizeCalendarInlineSettingsForConfigure: () => normalizeCalendarInlineSettingsForConfigure,
  normalizeCalendarPopupConfigureValue: () => normalizeCalendarPopupConfigureValue,
  normalizeCalendarPopupSettings: () => normalizeCalendarPopupSettings,
  projectCalendarBlockPopupHosts: () => projectCalendarBlockPopupHosts,
  projectCalendarBlockPopupHostsInTree: () => projectCalendarBlockPopupHostsInTree,
  replaceCalendarStoredPopupSettings: () => replaceCalendarStoredPopupSettings,
  resolveCalendarInitialPopupOverride: () => resolveCalendarInitialPopupOverride,
  stripCalendarPopupTargetSettingsForResourceChange: () => stripCalendarPopupTargetSettingsForResourceChange
});
module.exports = __toCommonJS(hidden_popup_calendar_exports);
var import_lodash = __toESM(require("lodash"));
var import_service_utils = require("./service-utils");
var import_hidden_popup_contract = require("./hidden-popup-contract");
const CALENDAR_POPUP_ACTION_KEYS = ["quickCreateAction", "eventViewAction"];
const CALENDAR_POPUP_PROP_KEYS = ["quickCreatePopupSettings", "eventPopupSettings"];
function normalizeCalendarPopupSettings(actionKey, popupSettings) {
  return (0, import_hidden_popup_contract.normalizeHiddenPopupSettings)(popupSettings, {
    collectionOnly: actionKey === "quickCreateAction",
    preserveApplyBlueprintDefaults: true
  });
}
function getCalendarPopupActionUse(actionKey) {
  return actionKey === "quickCreateAction" ? "CalendarQuickCreateActionModel" : "CalendarEventViewActionModel";
}
function getCalendarPopupPropKey(actionKey) {
  return actionKey === "quickCreateAction" ? "quickCreatePopupSettings" : "eventPopupSettings";
}
function getCalendarPopupActionUid(calendarUid, actionKey) {
  return `${calendarUid}-${actionKey}`;
}
function getCalendarBlockResourceInit(blockNode) {
  const resourceInit = import_lodash.default.cloneDeep(import_lodash.default.get(blockNode, ["stepParams", "resourceSettings", "init"]) || {});
  if (resourceInit.collectionName && !resourceInit.dataSourceKey) {
    resourceInit.dataSourceKey = "main";
  }
  return resourceInit;
}
function getCalendarPopupStoredSettings(blockNode, actionKey) {
  const propKey = getCalendarPopupPropKey(actionKey);
  const stepParamsPath = ["stepParams", "calendarSettings", propKey];
  const rawSettings = import_lodash.default.has(blockNode, stepParamsPath) ? import_lodash.default.isPlainObject(import_lodash.default.get(blockNode, stepParamsPath)) ? import_lodash.default.get(blockNode, stepParamsPath) : {} : import_lodash.default.isPlainObject(import_lodash.default.get(blockNode, ["props", propKey])) ? import_lodash.default.get(blockNode, ["props", propKey]) : {};
  return normalizeCalendarPopupSettings(actionKey, import_lodash.default.cloneDeep(rawSettings || {}));
}
async function replaceCalendarStoredPopupSettings(runtime, blockNode, actionKey, popupSettings, transaction) {
  if (!(blockNode == null ? void 0 : blockNode.uid)) {
    return;
  }
  const propKey = getCalendarPopupPropKey(actionKey);
  const path = ["calendarSettings", propKey];
  const nextStepParams = import_lodash.default.cloneDeep(blockNode.stepParams || {});
  if (Object.keys(popupSettings || {}).length) {
    import_lodash.default.set(nextStepParams, path, import_lodash.default.cloneDeep(popupSettings));
  } else {
    (0, import_hidden_popup_contract.unsetHiddenPopupPayloadPathAndPruneEmptyParents)(nextStepParams, path);
  }
  if (import_lodash.default.isEqual(nextStepParams, blockNode.stepParams || {})) {
    return;
  }
  await runtime.repository.patch(
    {
      uid: blockNode.uid,
      stepParams: nextStepParams
    },
    { transaction }
  );
  blockNode.stepParams = nextStepParams;
}
async function persistCalendarPopupSettingsFromOpenView(runtime, blockNode, actionKey, openView, popupSettings, transaction) {
  const storedSettings = (0, import_hidden_popup_contract.buildPersistedHiddenPopupSettingsFromOpenView)({
    openView,
    popupSettings,
    expectedHostUid: getCalendarPopupActionUid(blockNode.uid, actionKey),
    normalizePopupSettings: (settings) => normalizeCalendarPopupSettings(actionKey, settings)
  });
  await replaceCalendarStoredPopupSettings(runtime, blockNode, actionKey, storedSettings, transaction);
}
function buildCalendarInitialStepParams(input) {
  const nextStepParams = import_lodash.default.cloneDeep(input.stepParams || {});
  const props = input.props || {};
  const settings = input.settings || {};
  const quickCreatePopupSettings = import_lodash.default.has(nextStepParams, ["calendarSettings", "quickCreatePopupSettings"]) ? import_lodash.default.get(nextStepParams, ["calendarSettings", "quickCreatePopupSettings"]) : import_lodash.default.isPlainObject(props.quickCreatePopupSettings) ? props.quickCreatePopupSettings : import_lodash.default.isPlainObject(props.quickCreatePopup) ? props.quickCreatePopup : import_lodash.default.isPlainObject(settings.quickCreatePopupSettings) ? settings.quickCreatePopupSettings : import_lodash.default.isPlainObject(settings.quickCreatePopup) ? settings.quickCreatePopup : void 0;
  const eventPopupSettings = import_lodash.default.has(nextStepParams, ["calendarSettings", "eventPopupSettings"]) ? import_lodash.default.get(nextStepParams, ["calendarSettings", "eventPopupSettings"]) : import_lodash.default.isPlainObject(props.eventPopupSettings) ? props.eventPopupSettings : import_lodash.default.isPlainObject(props.eventPopup) ? props.eventPopup : import_lodash.default.isPlainObject(settings.eventPopupSettings) ? settings.eventPopupSettings : import_lodash.default.isPlainObject(settings.eventPopup) ? settings.eventPopup : void 0;
  if (import_lodash.default.isPlainObject(quickCreatePopupSettings)) {
    import_lodash.default.set(
      nextStepParams,
      ["calendarSettings", "quickCreatePopupSettings"],
      normalizeCalendarPopupSettings("quickCreateAction", import_lodash.default.cloneDeep(quickCreatePopupSettings))
    );
  }
  if (import_lodash.default.isPlainObject(eventPopupSettings)) {
    import_lodash.default.set(
      nextStepParams,
      ["calendarSettings", "eventPopupSettings"],
      normalizeCalendarPopupSettings("eventViewAction", import_lodash.default.cloneDeep(eventPopupSettings))
    );
  }
  return nextStepParams;
}
function buildCalendarPopupOpenView(input) {
  const actionUid = getCalendarPopupActionUid(input.blockNode.uid, input.actionKey);
  const resourceInit = input.resourceInit || getCalendarBlockResourceInit(input.blockNode);
  return (0, import_hidden_popup_contract.buildHiddenPopupOpenView)({
    actionUid,
    resourceInit,
    popupSettings: input.popupSettings ? input.popupSettings : getCalendarPopupStoredSettings(input.blockNode, input.actionKey),
    normalizePopupSettings: (popupSettings) => normalizeCalendarPopupSettings(input.actionKey, popupSettings)
  });
}
function mergeCalendarPopupSettings(actionKey, current, value) {
  if (!import_lodash.default.isPlainObject(value)) {
    return {};
  }
  const currentBase = import_lodash.default.isPlainObject(current) && (0, import_hidden_popup_contract.hasExplicitHiddenPopupTargetSettings)(value) ? import_lodash.default.pick(import_lodash.default.cloneDeep(current), import_hidden_popup_contract.HIDDEN_POPUP_DISPLAY_KEYS) : current;
  const merged = import_lodash.default.isPlainObject(currentBase) ? { ...import_lodash.default.cloneDeep(currentBase), ...import_lodash.default.cloneDeep(value) } : value;
  return normalizeCalendarPopupSettings(actionKey, merged);
}
function stripCalendarPopupTargetSettingsForResourceChange(actionKey, popupSettings) {
  return normalizeCalendarPopupSettings(
    actionKey,
    (0, import_hidden_popup_contract.stripHiddenPopupTargetSettings)(popupSettings, { disableTemplateAutoBind: true })
  );
}
function mergeCalendarPopupDisplaySettings(actionKey, openView, popupSettings, fallbackOpenView, options = {}) {
  return (0, import_hidden_popup_contract.mergeHiddenPopupDisplaySettings)({
    openView,
    popupSettings,
    fallbackOpenView,
    normalizePopupSettings: (settings) => normalizeCalendarPopupSettings(actionKey, settings),
    preserveOpenViewDisplay: options.preserveOpenViewDisplay,
    preserveOpenViewDisplayFallback: options.preserveOpenViewDisplayFallback
  });
}
async function ensureCalendarPopupHostDefaultContent(runtime, input) {
  return runtime.ensurePopupHostDefaultContent({
    actionName: `calendar ${input.actionKey}`,
    actionUid: input.actionUid,
    popupSettings: input.popupSettings,
    transaction: input.transaction,
    hasCurrentRecord: input.actionKey === "eventViewAction",
    mergeDisplaySettings: (openView, popupSettings, fallbackOpenView) => mergeCalendarPopupDisplaySettings(input.actionKey, openView, popupSettings, fallbackOpenView)
  });
}
async function buildCalendarPopupOpenViewWithTemplate(runtime, input) {
  var _a, _b;
  const actionUid = getCalendarPopupActionUid(input.blockNode.uid, input.actionKey);
  const openView = buildCalendarPopupOpenView(input);
  return runtime.buildPopupOpenViewWithTemplate({
    actionName: `calendar ${input.actionKey}`,
    actionUid,
    openView,
    popupSettings: input.popupSettings,
    existingHost: (0, import_service_utils.getSingleNodeSubModel)((_b = (_a = input.blockNode) == null ? void 0 : _a.subModels) == null ? void 0 : _b[input.actionKey]),
    transaction: input.transaction,
    hasCurrentRecord: input.actionKey === "eventViewAction",
    normalizePopupSettings: (popupSettings) => normalizeCalendarPopupSettings(input.actionKey, popupSettings),
    mergeDisplaySettings: (nextOpenView, popupSettings, fallbackOpenView) => mergeCalendarPopupDisplaySettings(input.actionKey, nextOpenView, popupSettings, fallbackOpenView)
  });
}
async function normalizeCalendarPopupConfigureValue(input) {
  if (import_lodash.default.isUndefined(input.value)) {
    return void 0;
  }
  if (import_lodash.default.isNull(input.value)) {
    return {};
  }
  const actionUid = getCalendarPopupActionUid(input.blockUid, input.actionKey);
  const normalized = await input.normalizeOpenView(input.actionName, input.value, {
    transaction: input.transaction,
    popupTemplateHostUid: actionUid,
    popupActionContext: {
      hasCurrentRecord: input.actionKey === "eventViewAction"
    }
  });
  return normalizeCalendarPopupSettings(input.actionKey, {
    ...normalized || {},
    ...import_lodash.default.isPlainObject(input.value) && input.value.tryTemplate === false ? { tryTemplate: false } : {}
  });
}
function normalizeCalendarInlineSettingsForConfigure(settings) {
  if (!import_lodash.default.isPlainObject(settings)) {
    return settings;
  }
  const nextSettings = import_lodash.default.cloneDeep(settings);
  if (Object.prototype.hasOwnProperty.call(nextSettings, "quickCreatePopupSettings")) {
    if (!Object.prototype.hasOwnProperty.call(nextSettings, "quickCreatePopup")) {
      nextSettings.quickCreatePopup = nextSettings.quickCreatePopupSettings;
    }
    delete nextSettings.quickCreatePopupSettings;
  }
  if (Object.prototype.hasOwnProperty.call(nextSettings, "eventPopupSettings")) {
    if (!Object.prototype.hasOwnProperty.call(nextSettings, "eventPopup")) {
      nextSettings.eventPopup = nextSettings.eventPopupSettings;
    }
    delete nextSettings.eventPopupSettings;
  }
  return nextSettings;
}
function resolveCalendarInitialPopupOverride(settings, primaryKey, legacyKey) {
  if (!import_lodash.default.isPlainObject(settings)) {
    return void 0;
  }
  if (import_lodash.default.isPlainObject(settings[primaryKey])) {
    return settings[primaryKey];
  }
  if (import_lodash.default.isPlainObject(settings[legacyKey])) {
    return settings[legacyKey];
  }
  return void 0;
}
async function ensureCalendarBlockPopupHosts(runtime, blockNode, transaction, popupSettingsOverrides, options = {}) {
  var _a, _b;
  if (!(blockNode == null ? void 0 : blockNode.uid) || blockNode.use !== "CalendarBlockModel") {
    return blockNode;
  }
  const resourceInit = getCalendarBlockResourceInit(blockNode);
  let changed = false;
  for (const actionKey of CALENDAR_POPUP_ACTION_KEYS) {
    let existing = (0, import_service_utils.getSingleNodeSubModel)((_a = blockNode.subModels) == null ? void 0 : _a[actionKey]);
    const expectedUid = getCalendarPopupActionUid(blockNode.uid, actionKey);
    const expectedUse = getCalendarPopupActionUse(actionKey);
    const popupSettingsOverride = (0, import_hidden_popup_contract.resolveHiddenPopupSettingsOverride)(popupSettingsOverrides, actionKey);
    const explicitPopupSettingsProvided = !!popupSettingsOverrides && Object.prototype.hasOwnProperty.call(popupSettingsOverrides, actionKey);
    const popupSettings = popupSettingsOverride ?? getCalendarPopupStoredSettings(blockNode, actionKey);
    const seedOpenView = buildCalendarPopupOpenView({
      blockNode,
      actionKey,
      resourceInit,
      popupSettings
    });
    const displayFallbackOpenView = ((_b = options.displayFallbackOpenViews) == null ? void 0 : _b[actionKey]) || seedOpenView;
    const shouldReplaceExisting = (existing == null ? void 0 : existing.uid) && existing.uid !== expectedUid;
    if (!(existing == null ? void 0 : existing.uid) || shouldReplaceExisting || existing.use !== expectedUse) {
      if (shouldReplaceExisting) {
        await runtime.removeNodeTreeWithBindings(existing.uid, transaction);
      }
      await runtime.repository.upsertModel(
        {
          parentId: blockNode.uid,
          subKey: actionKey,
          subType: "object",
          ...(existing == null ? void 0 : existing.uid) === expectedUid ? import_lodash.default.cloneDeep(existing) : {},
          uid: expectedUid,
          use: expectedUse,
          stepParams: (0, import_hidden_popup_contract.buildHiddenPopupActionStepParams)(
            (existing == null ? void 0 : existing.uid) === expectedUid ? existing.stepParams : {},
            seedOpenView
          )
        },
        { transaction }
      );
      existing = await runtime.repository.findModelById(expectedUid, {
        transaction,
        includeAsyncNode: true
      });
      if (String((seedOpenView == null ? void 0 : seedOpenView.popupTemplateUid) || "").trim()) {
        await runtime.clearFlowTemplateUsagesForNodeTree(expectedUid, transaction);
        await runtime.syncFlowTemplateUsagesForNodeTree(expectedUid, transaction);
      }
      changed = true;
    }
    const currentOpenView = (0, import_hidden_popup_contract.resolveHiddenPopupHostOpenView)(existing);
    const normalizedCurrentOpenView = normalizeCalendarPopupSettings(actionKey, currentOpenView);
    const popupOpenViewTransition = await (0, import_hidden_popup_contract.resolveHiddenPopupOpenViewTransitionWithCandidate)({
      existing,
      expectedUid,
      expectedUse,
      popupSettings,
      seedOpenView,
      currentOpenView,
      normalizedCurrentOpenView,
      displayFallbackOpenView,
      allowResourceMismatchRebuild: explicitPopupSettingsProvided,
      mergeDisplaySettings: (openView2, popupSettings2, fallbackOpenView, mergeOptions) => mergeCalendarPopupDisplaySettings(actionKey, openView2, popupSettings2, fallbackOpenView, mergeOptions),
      resolveCandidateOpenView: () => buildCalendarPopupOpenViewWithTemplate(runtime, {
        blockNode,
        actionKey,
        resourceInit,
        popupSettings,
        transaction
      })
    });
    const { openView } = popupOpenViewTransition;
    const shouldUpsert = !(existing == null ? void 0 : existing.uid) || existing.use !== expectedUse || !import_lodash.default.isEqual(currentOpenView, openView);
    if (shouldUpsert) {
      if ((existing == null ? void 0 : existing.uid) && existing.uid === expectedUid && !import_lodash.default.isEqual(currentOpenView, openView)) {
        await runtime.reconcilePopupOpenViewTransition(expectedUid, currentOpenView, openView, transaction);
      }
      const shouldRefreshTemplateUsages = !import_lodash.default.isEqual(currentOpenView, openView);
      const nextActionNode = {
        ...(existing == null ? void 0 : existing.uid) && existing.uid === expectedUid ? import_lodash.default.cloneDeep(existing) : {},
        uid: expectedUid,
        use: expectedUse,
        stepParams: (0, import_hidden_popup_contract.buildHiddenPopupActionStepParams)(
          (existing == null ? void 0 : existing.uid) === expectedUid ? existing.stepParams : {},
          openView
        )
      };
      await runtime.repository.upsertModel(
        {
          parentId: blockNode.uid,
          subKey: actionKey,
          subType: "object",
          ...nextActionNode
        },
        { transaction }
      );
      if (shouldRefreshTemplateUsages || String((openView == null ? void 0 : openView.popupTemplateUid) || "").trim()) {
        await runtime.clearFlowTemplateUsagesForNodeTree(expectedUid, transaction);
        if (String((openView == null ? void 0 : openView.popupTemplateUid) || "").trim()) {
          await runtime.syncFlowTemplateUsagesForNodeTree(expectedUid, transaction);
        }
      }
      changed = true;
    }
    const completedDefaultContent = await ensureCalendarPopupHostDefaultContent(runtime, {
      actionKey,
      actionUid: expectedUid,
      popupSettings,
      transaction
    });
    if (completedDefaultContent) {
      existing = await runtime.repository.findModelById(expectedUid, {
        transaction,
        includeAsyncNode: true
      });
    }
    const finalOpenView = completedDefaultContent ? (0, import_hidden_popup_contract.resolveHiddenPopupHostOpenView)(existing) : openView;
    await persistCalendarPopupSettingsFromOpenView(
      runtime,
      blockNode,
      actionKey,
      finalOpenView,
      popupSettings,
      transaction
    );
    if (existing == null ? void 0 : existing.uid) {
      blockNode.subModels = {
        ...blockNode.subModels || {},
        [actionKey]: existing
      };
    }
    changed = completedDefaultContent || changed;
  }
  if (!changed) {
    return blockNode;
  }
  const refreshed = await runtime.repository.findModelById(blockNode.uid, {
    transaction,
    includeAsyncNode: true
  });
  if (refreshed == null ? void 0 : refreshed.uid) {
    return refreshed;
  }
  return blockNode;
}
function projectCalendarBlockPopupHosts(node) {
  var _a;
  if (!node || typeof node !== "object") {
    return node;
  }
  const current = node;
  if (!(current == null ? void 0 : current.uid) || current.use !== "CalendarBlockModel") {
    return node;
  }
  const resourceInit = getCalendarBlockResourceInit(current);
  let nextSubModels = current.subModels;
  let changed = false;
  for (const actionKey of CALENDAR_POPUP_ACTION_KEYS) {
    const existing = (0, import_service_utils.getSingleNodeSubModel)((_a = current.subModels) == null ? void 0 : _a[actionKey]);
    const expectedUid = getCalendarPopupActionUid(current.uid, actionKey);
    const expectedUse = getCalendarPopupActionUse(actionKey);
    const popupSettings = getCalendarPopupStoredSettings(current, actionKey);
    const persistedOpenView = (0, import_hidden_popup_contract.resolveHiddenPopupHostOpenView)(existing);
    const seedOpenView = buildCalendarPopupOpenView({
      blockNode: current,
      actionKey,
      resourceInit,
      popupSettings
    });
    const currentOpenView = (0, import_hidden_popup_contract.resolveHiddenPopupHostOpenView)(existing);
    const normalizedCurrentOpenView = normalizeCalendarPopupSettings(actionKey, currentOpenView);
    const { openView } = (0, import_hidden_popup_contract.resolveHiddenPopupOpenViewTransition)({
      existing,
      expectedUid,
      expectedUse,
      popupSettings,
      seedOpenView,
      candidateOpenView: seedOpenView,
      currentOpenView,
      normalizedCurrentOpenView,
      preservedOpenView: persistedOpenView,
      resourceMismatchDisplayFallbackOpenView: (0, import_hidden_popup_contract.buildHiddenPopupResourceMismatchDisplayFallbackOpenView)({
        normalizedCurrentOpenView,
        resourceInit,
        popupSettings,
        stripPopupTargetSettingsForResourceChange: (popupSettings2) => stripCalendarPopupTargetSettingsForResourceChange(actionKey, popupSettings2),
        buildOpenView: ({ resourceInit: resourceInit2, popupSettings: popupSettings2 }) => buildCalendarPopupOpenView({
          blockNode: current,
          actionKey,
          resourceInit: resourceInit2,
          popupSettings: popupSettings2
        })
      }),
      allowResourceMismatchRebuild: true,
      mergeDisplaySettings: (openView2, popupSettings2, fallbackOpenView, mergeOptions) => mergeCalendarPopupDisplaySettings(actionKey, openView2, popupSettings2, fallbackOpenView, mergeOptions)
    });
    if ((existing == null ? void 0 : existing.uid) === expectedUid && existing.use === expectedUse && import_lodash.default.isEqual(currentOpenView, openView)) {
      continue;
    }
    const nextActionNode = {
      ...(existing == null ? void 0 : existing.uid) === expectedUid ? import_lodash.default.cloneDeep(existing) : {},
      uid: expectedUid,
      use: expectedUse,
      stepParams: (0, import_hidden_popup_contract.buildHiddenPopupActionStepParams)((existing == null ? void 0 : existing.uid) === expectedUid ? existing.stepParams : {}, openView)
    };
    if (!changed) {
      nextSubModels = {
        ...current.subModels || {}
      };
      changed = true;
    }
    nextSubModels[actionKey] = nextActionNode;
  }
  if (!changed) {
    return node;
  }
  return {
    ...current,
    subModels: nextSubModels
  };
}
function projectCalendarBlockPopupHostsInTree(node) {
  if (!node || typeof node !== "object") {
    return node;
  }
  let current = projectCalendarBlockPopupHosts(node);
  let nextSubModels = current == null ? void 0 : current.subModels;
  let changed = current !== node;
  if (!nextSubModels || typeof nextSubModels !== "object") {
    return current;
  }
  for (const [subKey, value] of Object.entries(nextSubModels)) {
    if (Array.isArray(value)) {
      const nextItems = [];
      let itemsChanged = false;
      for (const item of value) {
        const nextItem = projectCalendarBlockPopupHostsInTree(item);
        nextItems.push(nextItem);
        itemsChanged = itemsChanged || nextItem !== item;
      }
      if (!itemsChanged) {
        continue;
      }
      if (!changed) {
        current = {
          ...current,
          subModels: {
            ...nextSubModels
          }
        };
        nextSubModels = current.subModels;
        changed = true;
      }
      nextSubModels[subKey] = nextItems;
      continue;
    }
    const nextValue = projectCalendarBlockPopupHostsInTree(value);
    if (nextValue === value) {
      continue;
    }
    if (!changed) {
      current = {
        ...current,
        subModels: {
          ...nextSubModels
        }
      };
      nextSubModels = current.subModels;
      changed = true;
    }
    nextSubModels[subKey] = nextValue;
  }
  return current;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  CALENDAR_POPUP_ACTION_KEYS,
  CALENDAR_POPUP_PROP_KEYS,
  buildCalendarInitialStepParams,
  buildCalendarPopupOpenView,
  ensureCalendarBlockPopupHosts,
  getCalendarBlockResourceInit,
  getCalendarPopupActionUid,
  getCalendarPopupActionUse,
  getCalendarPopupPropKey,
  getCalendarPopupStoredSettings,
  mergeCalendarPopupSettings,
  normalizeCalendarInlineSettingsForConfigure,
  normalizeCalendarPopupConfigureValue,
  normalizeCalendarPopupSettings,
  projectCalendarBlockPopupHosts,
  projectCalendarBlockPopupHostsInTree,
  replaceCalendarStoredPopupSettings,
  resolveCalendarInitialPopupOverride,
  stripCalendarPopupTargetSettingsForResourceChange
});
