/**
 * 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_contract_exports = {};
__export(hidden_popup_contract_exports, {
  HIDDEN_POPUP_DISPLAY_KEYS: () => HIDDEN_POPUP_DISPLAY_KEYS,
  HIDDEN_POPUP_TEMPLATE_TARGET_KEYS: () => HIDDEN_POPUP_TEMPLATE_TARGET_KEYS,
  OPEN_VIEW_MODE_ALIASES: () => OPEN_VIEW_MODE_ALIASES,
  attachHiddenPopupDefaults: () => attachHiddenPopupDefaults,
  buildHiddenPopupActionStepParams: () => buildHiddenPopupActionStepParams,
  buildHiddenPopupOpenView: () => buildHiddenPopupOpenView,
  buildHiddenPopupResourceMismatchDisplayFallbackOpenView: () => buildHiddenPopupResourceMismatchDisplayFallbackOpenView,
  buildImplicitHiddenPopupDefaultContent: () => buildImplicitHiddenPopupDefaultContent,
  buildPersistedHiddenPopupSettingsFromOpenView: () => buildPersistedHiddenPopupSettingsFromOpenView,
  getHiddenPopupDisplaySettings: () => getHiddenPopupDisplaySettings,
  getHiddenPopupOpenViewDisplayFallback: () => getHiddenPopupOpenViewDisplayFallback,
  getHiddenPopupOpenViewDisplayOverrides: () => getHiddenPopupOpenViewDisplayOverrides,
  hasExplicitHiddenPopupResourceTargetSettings: () => hasExplicitHiddenPopupResourceTargetSettings,
  hasExplicitHiddenPopupTargetSettings: () => hasExplicitHiddenPopupTargetSettings,
  hiddenPopupHostHasLocalContent: () => hiddenPopupHostHasLocalContent,
  hiddenPopupOpenViewResourceDiffers: () => hiddenPopupOpenViewResourceDiffers,
  mergeHiddenPopupDisplaySettings: () => mergeHiddenPopupDisplaySettings,
  mergeHiddenPopupOpenViewDisplayOverrides: () => mergeHiddenPopupOpenViewDisplayOverrides,
  normalizeBlockHiddenPopupSettings: () => normalizeBlockHiddenPopupSettings,
  normalizeHiddenPopupScalarSettings: () => normalizeHiddenPopupScalarSettings,
  normalizeHiddenPopupSettings: () => normalizeHiddenPopupSettings,
  resolveHiddenPopupHostOpenView: () => resolveHiddenPopupHostOpenView,
  resolveHiddenPopupOpenViewTransition: () => resolveHiddenPopupOpenViewTransition,
  resolveHiddenPopupOpenViewTransitionWithCandidate: () => resolveHiddenPopupOpenViewTransitionWithCandidate,
  resolveHiddenPopupSettingsOverride: () => resolveHiddenPopupSettingsOverride,
  shouldAutoBindHiddenPopupTemplate: () => shouldAutoBindHiddenPopupTemplate,
  shouldPreserveExplicitHiddenPopupTemplateOpenView: () => shouldPreserveExplicitHiddenPopupTemplateOpenView,
  shouldPreserveHiddenPopupOpenView: () => shouldPreserveHiddenPopupOpenView,
  stripHiddenPopupTargetSettings: () => stripHiddenPopupTargetSettings,
  unsetHiddenPopupPayloadPathAndPruneEmptyParents: () => unsetHiddenPopupPayloadPathAndPruneEmptyParents
});
module.exports = __toCommonJS(hidden_popup_contract_exports);
var import_lodash = __toESM(require("lodash"));
var import_defaults = require("./blueprint/defaults");
var import_default_block_actions = require("./default-block-actions");
var import_service_utils = require("./service-utils");
var import_template_service_utils = require("./template-service-utils");
const OPEN_VIEW_MODE_ALIASES = {
  modal: "dialog",
  page: "embed"
};
const HIDDEN_POPUP_TEMPLATE_TARGET_KEYS = [
  "template",
  "tryTemplate",
  "popupTemplateUid",
  "popupTemplateContext",
  "popupTemplateMode",
  "popupTemplateHasFilterByTk",
  "popupTemplateHasSourceId",
  "associationName",
  "filterByTk",
  "sourceId"
];
const HIDDEN_POPUP_DISPLAY_OMIT_KEYS = [
  ...HIDDEN_POPUP_TEMPLATE_TARGET_KEYS,
  "uid",
  "collectionName",
  "dataSourceKey"
];
const HIDDEN_POPUP_TEMPLATE_CLEAR_KEYS = [
  "popupTemplateUid",
  "popupTemplateContext",
  "popupTemplateMode",
  "popupTemplateHasFilterByTk",
  "popupTemplateHasSourceId",
  "uid"
];
const HIDDEN_POPUP_DISPLAY_KEYS = ["mode", "size", "pageModelClass", "title"];
const HIDDEN_POPUP_RESOURCE_TARGET_KEYS = [
  "template",
  "popupTemplateUid",
  "popupTemplateContext",
  "popupTemplateMode",
  "popupTemplateHasFilterByTk",
  "popupTemplateHasSourceId",
  "collectionName",
  "dataSourceKey",
  "uid",
  "associationName",
  "filterByTk",
  "sourceId"
];
function normalizeHiddenPopupScalarSettings(popupSettings) {
  for (const key of [
    "mode",
    "size",
    "pageModelClass",
    "collectionName",
    "dataSourceKey",
    "associationName",
    "filterByTk",
    "sourceId",
    "title",
    "popupTemplateMode"
  ]) {
    if (!Object.prototype.hasOwnProperty.call(popupSettings, key)) {
      continue;
    }
    const value = popupSettings[key];
    if (import_lodash.default.isNull(value) || import_lodash.default.isUndefined(value)) {
      delete popupSettings[key];
      continue;
    }
    if (typeof value !== "string") {
      continue;
    }
    const trimmed = value.trim();
    if (!trimmed) {
      delete popupSettings[key];
      continue;
    }
    popupSettings[key] = key === "mode" ? OPEN_VIEW_MODE_ALIASES[trimmed] || trimmed : trimmed;
  }
  if (Object.prototype.hasOwnProperty.call(popupSettings, "uid") && typeof popupSettings.uid === "string") {
    const uidValue = popupSettings.uid.trim();
    if (uidValue) {
      popupSettings.uid = uidValue;
    } else {
      delete popupSettings.uid;
    }
  }
}
function attachHiddenPopupDefaults(popup, defaultsMetadata) {
  if (!defaultsMetadata || !import_lodash.default.isPlainObject(popup)) {
    return popup;
  }
  return (0, import_defaults.attachFlowSurfaceApplyBlueprintPopupDefaults)(popup, defaultsMetadata);
}
function normalizeBlockHiddenPopupSettings(settings, blockContext, keys, defaultsMetadata, assertPopupObject) {
  if (!import_lodash.default.isPlainObject(settings)) {
    return settings;
  }
  const nextSettings = import_lodash.default.cloneDeep(settings);
  for (const key of keys) {
    if (!Object.prototype.hasOwnProperty.call(settings, key)) {
      continue;
    }
    const value = settings[key];
    if (!import_lodash.default.isPlainObject(value) && !import_lodash.default.isNull(value)) {
      if (assertPopupObject) {
        assertPopupObject(`${blockContext}.settings.${key} must be an object`);
      } else {
        throw new Error(`${blockContext}.settings.${key} must be an object`);
      }
    }
    nextSettings[key] = attachHiddenPopupDefaults(value, defaultsMetadata);
  }
  return nextSettings;
}
function clearHiddenPopupTemplateBinding(popupSettings) {
  for (const key of HIDDEN_POPUP_TEMPLATE_CLEAR_KEYS) {
    delete popupSettings[key];
  }
  popupSettings.tryTemplate = false;
}
function hasRecordScopedHiddenPopupTemplateBinding(popupSettings) {
  return !!popupSettings.popupTemplateHasFilterByTk || !!popupSettings.popupTemplateHasSourceId || !import_lodash.default.isUndefined(popupSettings.filterByTk) || !import_lodash.default.isUndefined(popupSettings.sourceId) || !!String(popupSettings.associationName || "").trim();
}
function normalizeHiddenPopupSettings(popupSettings, options = {}) {
  const nextParams = import_lodash.default.cloneDeep(popupSettings || {});
  if (!options.preserveApplyBlueprintDefaults) {
    delete nextParams[import_defaults.FLOW_SURFACE_APPLY_BLUEPRINT_POPUP_DEFAULTS_KEY];
  }
  normalizeHiddenPopupScalarSettings(nextParams);
  const popupTemplateUidProvided = Object.prototype.hasOwnProperty.call(nextParams, "popupTemplateUid");
  const popupTemplateUid = typeof nextParams.popupTemplateUid === "string" ? nextParams.popupTemplateUid.trim() : nextParams.popupTemplateUid;
  if (popupTemplateUidProvided && (popupTemplateUid === null || popupTemplateUid === "")) {
    clearHiddenPopupTemplateBinding(nextParams);
  }
  if (options.collectionOnly) {
    if (hasRecordScopedHiddenPopupTemplateBinding(nextParams)) {
      clearHiddenPopupTemplateBinding(nextParams);
    }
    delete nextParams.associationName;
    delete nextParams.filterByTk;
    delete nextParams.sourceId;
  }
  const normalizedInvalidUids = new Set(
    (options.invalidUids || []).map((value) => String(value || "").trim()).filter(Boolean)
  );
  if (normalizedInvalidUids.size) {
    const normalizedUid = typeof nextParams.uid === "string" ? nextParams.uid.trim() : nextParams.uid;
    if (!normalizedUid || normalizedInvalidUids.has(String(normalizedUid))) {
      delete nextParams.uid;
    } else {
      nextParams.uid = normalizedUid;
    }
  }
  if (nextParams.tryTemplate !== false) {
    delete nextParams.tryTemplate;
  }
  return nextParams;
}
function stripHiddenPopupTargetSettings(popupSettings, options = {}) {
  const nextParams = import_lodash.default.cloneDeep(popupSettings || {});
  for (const key of HIDDEN_POPUP_RESOURCE_TARGET_KEYS) {
    delete nextParams[key];
  }
  if (options.disableTemplateAutoBind || (popupSettings == null ? void 0 : popupSettings.tryTemplate) === false) {
    nextParams.tryTemplate = false;
  }
  return nextParams;
}
function normalizeHiddenPopupResourceKey(value) {
  return String(value || "").trim();
}
function normalizeHiddenPopupDataSourceKey(value, collectionName) {
  return normalizeHiddenPopupResourceKey(value) || (collectionName ? "main" : "");
}
function hiddenPopupOpenViewResourceDiffers(openView, fallbackOpenView) {
  if (!import_lodash.default.isPlainObject(openView) || !import_lodash.default.isPlainObject(fallbackOpenView)) {
    return false;
  }
  const expectedCollectionName = normalizeHiddenPopupResourceKey(fallbackOpenView.collectionName);
  const currentCollectionName = normalizeHiddenPopupResourceKey(openView.collectionName);
  if (expectedCollectionName && currentCollectionName && expectedCollectionName !== currentCollectionName) {
    return true;
  }
  const expectedDataSourceKey = normalizeHiddenPopupDataSourceKey(
    fallbackOpenView.dataSourceKey,
    expectedCollectionName
  );
  const currentDataSourceKey = normalizeHiddenPopupDataSourceKey(openView.dataSourceKey, currentCollectionName);
  return !!expectedDataSourceKey && !!currentDataSourceKey && expectedDataSourceKey !== currentDataSourceKey;
}
function buildHiddenPopupOpenView(input) {
  const resourceInit = input.resourceInit || {};
  const defaults = (0, import_service_utils.buildDefinedPayload)({
    mode: "drawer",
    size: "medium",
    pageModelClass: "ChildPageModel",
    uid: input.actionUid,
    collectionName: resourceInit.collectionName,
    dataSourceKey: resourceInit.dataSourceKey || (resourceInit.collectionName ? "main" : void 0)
  });
  const current = input.normalizePopupSettings ? input.normalizePopupSettings(input.popupSettings) : normalizeHiddenPopupSettings(input.popupSettings);
  const openViewSettings = import_lodash.default.omit(current, ["tryTemplate", import_defaults.FLOW_SURFACE_APPLY_BLUEPRINT_POPUP_DEFAULTS_KEY]);
  return (0, import_service_utils.buildDefinedPayload)({
    ...defaults,
    ...openViewSettings,
    uid: openViewSettings.uid || defaults.uid,
    collectionName: openViewSettings.collectionName || defaults.collectionName,
    dataSourceKey: openViewSettings.dataSourceKey || defaults.dataSourceKey
  });
}
function hasExplicitHiddenPopupTargetSettings(popupSettings, defaultUid) {
  if (!import_lodash.default.isPlainObject(popupSettings)) {
    return false;
  }
  const normalizedDefaultUid = String(defaultUid || "").trim();
  const uid = String(popupSettings.uid || "").trim();
  if (uid && (!normalizedDefaultUid || uid !== normalizedDefaultUid)) {
    return true;
  }
  return HIDDEN_POPUP_TEMPLATE_TARGET_KEYS.some((key) => {
    if (!Object.prototype.hasOwnProperty.call(popupSettings, key)) {
      return false;
    }
    const value = popupSettings[key];
    if (key === "template") {
      return import_lodash.default.isPlainObject(value) && Object.keys(value).length > 0;
    }
    if (key === "tryTemplate") {
      return value === false;
    }
    return value !== void 0 && value !== null && value !== "";
  });
}
function hasExplicitHiddenPopupResourceTargetSettings(popupSettings, defaultUid) {
  if (!import_lodash.default.isPlainObject(popupSettings)) {
    return false;
  }
  const normalizedDefaultUid = String(defaultUid || "").trim();
  const uid = String(popupSettings.uid || "").trim();
  if (uid && (!normalizedDefaultUid || uid !== normalizedDefaultUid)) {
    return true;
  }
  return HIDDEN_POPUP_RESOURCE_TARGET_KEYS.some((key) => {
    if (key === "uid" || !Object.prototype.hasOwnProperty.call(popupSettings, key)) {
      return false;
    }
    const value = popupSettings[key];
    if (key === "template") {
      return import_lodash.default.isPlainObject(value) && Object.keys(value).length > 0;
    }
    return value !== void 0 && value !== null && value !== "";
  });
}
function shouldPreserveExplicitHiddenPopupTemplateOpenView(openView, popupSettings) {
  if (!import_lodash.default.isPlainObject(openView) || !import_lodash.default.isPlainObject(popupSettings)) {
    return false;
  }
  const expectedTemplateUid = String(
    (import_lodash.default.isPlainObject(popupSettings.template) ? popupSettings.template.uid : popupSettings.popupTemplateUid) || ""
  ).trim() || void 0;
  if (!expectedTemplateUid) {
    return false;
  }
  if (String(openView.popupTemplateUid || "").trim() !== expectedTemplateUid) {
    return false;
  }
  const expectedMode = String(
    (import_lodash.default.isPlainObject(popupSettings.template) ? popupSettings.template.mode : popupSettings.popupTemplateMode) || ""
  ).trim() || "reference";
  const openViewMode = String(openView.popupTemplateMode || "reference").trim() || "reference";
  if (openViewMode !== expectedMode) {
    return false;
  }
  if (expectedMode === "copy") {
    return !!openView.popupTemplateContext;
  }
  return true;
}
function shouldPreserveHiddenPopupOpenView(openView, expectedHostUid, popupSettings, fallbackOpenView) {
  if (!import_lodash.default.isPlainObject(openView)) {
    return false;
  }
  const hostUid = String(openView.uid || "").trim();
  const normalizedExpectedHostUid = String(expectedHostUid || "").trim();
  const hasExternalUid = !!hostUid && !!normalizedExpectedHostUid && hostUid !== normalizedExpectedHostUid;
  const hasTemplateBinding = !!String(openView.popupTemplateUid || "").trim() || !!openView.popupTemplateContext || !!String(openView.popupTemplateMode || "").trim();
  const hasExplicitDisplayOverride = HIDDEN_POPUP_DISPLAY_KEYS.some((key) => {
    if (!Object.prototype.hasOwnProperty.call(openView, key)) {
      return false;
    }
    return !import_lodash.default.isEqual(openView[key], fallbackOpenView == null ? void 0 : fallbackOpenView[key]);
  });
  if (hasExplicitHiddenPopupTargetSettings(popupSettings, expectedHostUid)) {
    if (shouldPreserveExplicitHiddenPopupTemplateOpenView(openView, popupSettings)) {
      return true;
    }
    return hasExplicitDisplayOverride && !hasTemplateBinding && !hasExternalUid;
  }
  return hasTemplateBinding || hasExternalUid || hasExplicitDisplayOverride;
}
function getHiddenPopupDisplaySettings(popupSettings, normalizePopupSettings) {
  return import_lodash.default.omit(
    normalizePopupSettings ? normalizePopupSettings(popupSettings || {}) : normalizeHiddenPopupSettings(popupSettings || {}),
    HIDDEN_POPUP_DISPLAY_OMIT_KEYS
  );
}
function getHiddenPopupOpenViewDisplayFallback(openView, fallbackOpenView) {
  return (0, import_service_utils.buildDefinedPayload)({
    mode: (openView == null ? void 0 : openView.mode) || (fallbackOpenView == null ? void 0 : fallbackOpenView.mode),
    size: (openView == null ? void 0 : openView.size) || (fallbackOpenView == null ? void 0 : fallbackOpenView.size),
    pageModelClass: (openView == null ? void 0 : openView.pageModelClass) || (fallbackOpenView == null ? void 0 : fallbackOpenView.pageModelClass),
    title: (openView == null ? void 0 : openView.title) || (fallbackOpenView == null ? void 0 : fallbackOpenView.title)
  });
}
function mergeHiddenPopupDisplaySettings(input) {
  const displaySettings = getHiddenPopupDisplaySettings(input.popupSettings, input.normalizePopupSettings);
  const merged = (0, import_service_utils.buildDefinedPayload)({
    ...input.openView,
    ...displaySettings,
    ...getHiddenPopupOpenViewDisplayFallback({ ...input.openView, ...displaySettings }, input.fallbackOpenView),
    uid: input.openView.uid,
    collectionName: input.openView.collectionName,
    dataSourceKey: input.openView.dataSourceKey,
    associationName: input.openView.associationName,
    filterByTk: input.openView.filterByTk,
    sourceId: input.openView.sourceId,
    popupTemplateUid: input.openView.popupTemplateUid,
    popupTemplateContext: input.openView.popupTemplateContext,
    popupTemplateMode: input.openView.popupTemplateMode,
    popupTemplateHasFilterByTk: input.openView.popupTemplateHasFilterByTk,
    popupTemplateHasSourceId: input.openView.popupTemplateHasSourceId
  });
  if (input.preserveOpenViewDisplay) {
    const displayFallback = input.preserveOpenViewDisplayFallback || input.fallbackOpenView;
    for (const key of HIDDEN_POPUP_DISPLAY_KEYS) {
      if (Object.prototype.hasOwnProperty.call(input.openView, key) && !import_lodash.default.isEqual(input.openView[key], displayFallback == null ? void 0 : displayFallback[key])) {
        merged[key] = input.openView[key];
      }
    }
  }
  return (0, import_service_utils.buildDefinedPayload)(merged);
}
function getHiddenPopupOpenViewDisplayOverrides(openView, fallbackOpenView) {
  if (!import_lodash.default.isPlainObject(openView)) {
    return {};
  }
  const overrides = {};
  for (const key of HIDDEN_POPUP_DISPLAY_KEYS) {
    if (Object.prototype.hasOwnProperty.call(openView, key) && !import_lodash.default.isEqual(openView[key], fallbackOpenView == null ? void 0 : fallbackOpenView[key])) {
      overrides[key] = openView[key];
    }
  }
  return (0, import_service_utils.buildDefinedPayload)(overrides);
}
function mergeHiddenPopupOpenViewDisplayOverrides(input) {
  const merged = import_lodash.default.cloneDeep(input.openView || {});
  if (!import_lodash.default.isPlainObject(input.displayOpenView)) {
    return (0, import_service_utils.buildDefinedPayload)(merged);
  }
  const displayFallback = input.displayFallbackOpenView || input.fallbackOpenView;
  Object.assign(merged, getHiddenPopupOpenViewDisplayOverrides(input.displayOpenView, displayFallback));
  return (0, import_service_utils.buildDefinedPayload)(merged);
}
function resolveHiddenPopupOpenViewTransition(input) {
  var _a;
  const canUseExisting = ((_a = input.existing) == null ? void 0 : _a.uid) === input.expectedUid && input.existing.use === input.expectedUse;
  const normalizedCurrentOpenView = import_lodash.default.isPlainObject(input.normalizedCurrentOpenView) ? input.normalizedCurrentOpenView : {};
  const currentOpenView = import_lodash.default.isPlainObject(input.currentOpenView) ? input.currentOpenView : normalizedCurrentOpenView;
  const preservedOpenView = import_lodash.default.isPlainObject(input.preservedOpenView) ? input.preservedOpenView : normalizedCurrentOpenView;
  const displayFallbackOpenView = input.displayFallbackOpenView || input.seedOpenView;
  const resourceMismatchDisplayFallbackOpenView = input.resourceMismatchDisplayFallbackOpenView || displayFallbackOpenView;
  const shouldRebuildForResourceMismatch = !!input.allowResourceMismatchRebuild && canUseExisting && !hasExplicitHiddenPopupResourceTargetSettings(input.popupSettings, input.expectedUid) && hiddenPopupOpenViewResourceDiffers(normalizedCurrentOpenView, input.seedOpenView);
  const shouldPreservePersistedOpenView = canUseExisting && !shouldRebuildForResourceMismatch && shouldPreserveHiddenPopupOpenView(preservedOpenView, input.expectedUid, input.popupSettings, input.seedOpenView);
  const openView = shouldPreservePersistedOpenView ? input.mergeDisplaySettings(preservedOpenView, input.popupSettings, input.seedOpenView, {
    preserveOpenViewDisplay: true,
    preserveOpenViewDisplayFallback: displayFallbackOpenView
  }) : (0, import_service_utils.buildDefinedPayload)({
    ...mergeHiddenPopupOpenViewDisplayOverrides({
      openView: input.candidateOpenView || input.seedOpenView,
      displayOpenView: shouldRebuildForResourceMismatch ? normalizedCurrentOpenView : null,
      fallbackOpenView: input.seedOpenView,
      displayFallbackOpenView: shouldRebuildForResourceMismatch ? resourceMismatchDisplayFallbackOpenView : displayFallbackOpenView
    })
  });
  return {
    openView,
    shouldRebuildForResourceMismatch,
    shouldPreservePersistedOpenView,
    currentOpenView,
    normalizedCurrentOpenView
  };
}
async function resolveHiddenPopupOpenViewTransitionWithCandidate(input) {
  const transition = resolveHiddenPopupOpenViewTransition(input);
  if (transition.shouldPreservePersistedOpenView) {
    return transition;
  }
  return resolveHiddenPopupOpenViewTransition({
    ...input,
    candidateOpenView: await input.resolveCandidateOpenView()
  });
}
function buildHiddenPopupResourceMismatchDisplayFallbackOpenView(input) {
  const normalizedCurrentOpenView = import_lodash.default.isPlainObject(input.normalizedCurrentOpenView) ? input.normalizedCurrentOpenView : {};
  const resourceInit = normalizedCurrentOpenView.collectionName ? (0, import_service_utils.buildDefinedPayload)({
    collectionName: normalizedCurrentOpenView.collectionName,
    dataSourceKey: normalizedCurrentOpenView.dataSourceKey
  }) : input.resourceInit;
  return input.buildOpenView({
    resourceInit,
    popupSettings: input.stripPopupTargetSettingsForResourceChange(input.popupSettings)
  });
}
function buildHiddenPopupActionStepParams(existingStepParams, openView) {
  const nextStepParams = import_lodash.default.cloneDeep(existingStepParams || {});
  const popupSettings = import_lodash.default.isPlainObject(nextStepParams.popupSettings) ? import_lodash.default.cloneDeep(nextStepParams.popupSettings) : {};
  popupSettings.openView = import_lodash.default.omit(import_lodash.default.cloneDeep(openView || {}), ["tryTemplate"]);
  nextStepParams.popupSettings = popupSettings;
  return nextStepParams;
}
function buildPersistedHiddenPopupSettingsFromOpenView(input) {
  const normalizedOpenView = input.normalizePopupSettings(import_lodash.default.cloneDeep(input.openView || {}));
  const normalizedExpectedHostUid = String(input.expectedHostUid || "").trim();
  const openViewUid = String(normalizedOpenView.uid || "").trim();
  const templateSettings = (0, import_service_utils.buildDefinedPayload)({
    popupTemplateUid: normalizedOpenView.popupTemplateUid,
    popupTemplateContext: normalizedOpenView.popupTemplateContext,
    popupTemplateMode: normalizedOpenView.popupTemplateMode,
    popupTemplateHasFilterByTk: normalizedOpenView.popupTemplateHasFilterByTk,
    popupTemplateHasSourceId: normalizedOpenView.popupTemplateHasSourceId
  });
  const targetSettings = (0, import_service_utils.buildDefinedPayload)({
    ...openViewUid && openViewUid !== normalizedExpectedHostUid ? { uid: openViewUid } : {},
    associationName: normalizedOpenView.associationName,
    filterByTk: normalizedOpenView.filterByTk,
    sourceId: normalizedOpenView.sourceId
  });
  const displaySettings = import_lodash.default.pick(input.normalizePopupSettings(input.popupSettings || {}), HIDDEN_POPUP_DISPLAY_KEYS);
  const explicitTryTemplateOptOut = import_lodash.default.isPlainObject(input.popupSettings) && input.popupSettings.tryTemplate === false;
  return input.normalizePopupSettings(
    (0, import_service_utils.buildDefinedPayload)({
      ...templateSettings,
      ...targetSettings,
      ...displaySettings,
      ...explicitTryTemplateOptOut ? { tryTemplate: false } : {}
    })
  );
}
function unsetHiddenPopupPayloadPathAndPruneEmptyParents(payload, path) {
  import_lodash.default.unset(payload, path);
  for (let index = path.length - 1; index > 0; index -= 1) {
    const parentPath = path.slice(0, index);
    const parentValue = import_lodash.default.get(payload, parentPath);
    if (!import_lodash.default.isPlainObject(parentValue) || Object.keys(parentValue).length > 0) {
      break;
    }
    import_lodash.default.unset(payload, parentPath);
  }
}
function buildImplicitHiddenPopupDefaultContent(popupSettings) {
  const defaultsMetadata = import_lodash.default.isPlainObject(popupSettings) ? import_lodash.default.pick(popupSettings, [import_defaults.FLOW_SURFACE_APPLY_BLUEPRINT_POPUP_DEFAULTS_KEY]) : {};
  const hasDefaultsMetadata = import_lodash.default.isPlainObject(defaultsMetadata[import_defaults.FLOW_SURFACE_APPLY_BLUEPRINT_POPUP_DEFAULTS_KEY]);
  if (import_lodash.default.isPlainObject(popupSettings) && popupSettings.tryTemplate === false && !hasDefaultsMetadata) {
    return void 0;
  }
  return {
    ...defaultsMetadata,
    tryTemplate: hasDefaultsMetadata ? (popupSettings == null ? void 0 : popupSettings.tryTemplate) === true : true,
    [import_default_block_actions.FLOW_SURFACE_INTERNAL_AUTO_SAVE_DEFAULT_POPUP_TEMPLATE_KEY]: true
  };
}
function hiddenPopupHostHasLocalContent(actionNode) {
  var _a, _b, _c, _d;
  const popupPage = (0, import_service_utils.getSingleNodeSubModel)((_a = actionNode == null ? void 0 : actionNode.subModels) == null ? void 0 : _a.page);
  const popupTab = import_lodash.default.castArray(((_b = popupPage == null ? void 0 : popupPage.subModels) == null ? void 0 : _b.tabs) || [])[0];
  const popupGrid = (0, import_service_utils.getSingleNodeSubModel)((_c = popupTab == null ? void 0 : popupTab.subModels) == null ? void 0 : _c.grid);
  return (0, import_service_utils.getNodeSubModelList)((_d = popupGrid == null ? void 0 : popupGrid.subModels) == null ? void 0 : _d.items).length > 0;
}
function shouldAutoBindHiddenPopupTemplate(openView, popupSettings, expectedHostUid, options = {}) {
  if (!import_lodash.default.isPlainObject(openView)) {
    return false;
  }
  const rawPopupSettings = import_lodash.default.isPlainObject(popupSettings) ? popupSettings : {};
  if (rawPopupSettings.tryTemplate === false) {
    return false;
  }
  const hostUid = String(openView.uid || "").trim();
  const normalizedExpectedHostUid = String(expectedHostUid || "").trim();
  const hasExternalUid = !!hostUid && !!normalizedExpectedHostUid && hostUid !== normalizedExpectedHostUid;
  if (!hasExternalUid && options.hostHasLocalContent) {
    return false;
  }
  return !String(openView.popupTemplateUid || "").trim() && !openView.popupTemplateContext && !String(openView.popupTemplateMode || "").trim() && !import_lodash.default.isPlainObject(openView.template) && !!hostUid && !hasExternalUid;
}
function resolveHiddenPopupHostOpenView(node) {
  for (const [flowKey, stepKey] of import_template_service_utils.POPUP_HOST_STEP_PARAM_PATHS) {
    const openView = import_lodash.default.get(node, ["stepParams", flowKey, stepKey]);
    if (import_lodash.default.isPlainObject(openView)) {
      return openView;
    }
  }
  return null;
}
function resolveHiddenPopupSettingsOverride(overrides, actionKey) {
  if (!overrides || !Object.prototype.hasOwnProperty.call(overrides, actionKey)) {
    return void 0;
  }
  return overrides[actionKey];
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  HIDDEN_POPUP_DISPLAY_KEYS,
  HIDDEN_POPUP_TEMPLATE_TARGET_KEYS,
  OPEN_VIEW_MODE_ALIASES,
  attachHiddenPopupDefaults,
  buildHiddenPopupActionStepParams,
  buildHiddenPopupOpenView,
  buildHiddenPopupResourceMismatchDisplayFallbackOpenView,
  buildImplicitHiddenPopupDefaultContent,
  buildPersistedHiddenPopupSettingsFromOpenView,
  getHiddenPopupDisplaySettings,
  getHiddenPopupOpenViewDisplayFallback,
  getHiddenPopupOpenViewDisplayOverrides,
  hasExplicitHiddenPopupResourceTargetSettings,
  hasExplicitHiddenPopupTargetSettings,
  hiddenPopupHostHasLocalContent,
  hiddenPopupOpenViewResourceDiffers,
  mergeHiddenPopupDisplaySettings,
  mergeHiddenPopupOpenViewDisplayOverrides,
  normalizeBlockHiddenPopupSettings,
  normalizeHiddenPopupScalarSettings,
  normalizeHiddenPopupSettings,
  resolveHiddenPopupHostOpenView,
  resolveHiddenPopupOpenViewTransition,
  resolveHiddenPopupOpenViewTransitionWithCandidate,
  resolveHiddenPopupSettingsOverride,
  shouldAutoBindHiddenPopupTemplate,
  shouldPreserveExplicitHiddenPopupTemplateOpenView,
  shouldPreserveHiddenPopupOpenView,
  stripHiddenPopupTargetSettings,
  unsetHiddenPopupPayloadPathAndPruneEmptyParents
});
