/*
 * Copyright 2025 Palantir Technologies, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { createOsdkObject } from "../../../object/convertWireToOsdkObjects/createOsdkObject.js";
import { ClientRef, ObjectDefRef, UnderlyingOsdkObject } from "../../../object/convertWireToOsdkObjects/InternalSymbols.js";
export function extractRdpFieldNames(rdpConfig) {
  if (!rdpConfig) {
    return new Set();
  }
  return new Set(Object.keys(rdpConfig));
}
function stripRdpFields(value, rdpFields) {
  if (rdpFields.size === 0) {
    return value;
  }
  const underlying = value[UnderlyingOsdkObject];
  const objectDef = value[ObjectDefRef];
  const newProps = {
    $apiName: underlying.$apiName,
    $objectType: underlying.$objectType,
    $primaryKey: underlying.$primaryKey,
    $title: underlying.$title
  };
  for (const key of Object.keys(underlying)) {
    if (key in objectDef.properties && !rdpFields.has(key)) {
      newProps[key] = underlying[key];
    }
  }
  return createOsdkObject(value[ClientRef], objectDef, newProps);
}
function isSuperset(superset, subset) {
  for (const field of subset) {
    if (!superset.has(field)) {
      return false;
    }
  }
  return true;
}
function filterToRdpFields(value, rdpFieldsToKeep, sourceRdpFields) {
  const underlying = value[UnderlyingOsdkObject];
  const objectDef = value[ObjectDefRef];
  const newProps = {
    $apiName: underlying.$apiName,
    $objectType: underlying.$objectType,
    $primaryKey: underlying.$primaryKey,
    $title: underlying.$title
  };
  for (const key of Object.keys(underlying)) {
    if (key in objectDef.properties) {
      const isRdpField = sourceRdpFields.has(key);
      if (!isRdpField || rdpFieldsToKeep.has(key)) {
        newProps[key] = underlying[key];
      }
    }
  }
  return createOsdkObject(value[ClientRef], objectDef, newProps);
}
export function mergeObjectFields(sourceValue, sourceRdpFields, targetRdpFields, targetCurrentValue) {
  if (targetRdpFields.size === 0) {
    return stripRdpFields(sourceValue, sourceRdpFields);
  }
  if (isSuperset(sourceRdpFields, targetRdpFields)) {
    if (sourceRdpFields.size === targetRdpFields.size) {
      return sourceValue;
    }
    return filterToRdpFields(sourceValue, targetRdpFields, sourceRdpFields);
  }
  const sourceUnderlying = sourceValue[UnderlyingOsdkObject];
  const objectDef = sourceValue[ObjectDefRef];
  const newProps = {
    $apiName: sourceUnderlying.$apiName,
    $objectType: sourceUnderlying.$objectType,
    $primaryKey: sourceUnderlying.$primaryKey,
    $title: sourceUnderlying.$title
  };
  for (const key of Object.keys(sourceUnderlying)) {
    if (key in objectDef.properties && (!sourceRdpFields.has(key) || targetRdpFields.has(key))) {
      newProps[key] = sourceUnderlying[key];
    }
  }
  if (targetCurrentValue) {
    const targetUnderlying = targetCurrentValue[UnderlyingOsdkObject];
    for (const field of targetRdpFields) {
      if (!sourceRdpFields.has(field) && field in targetUnderlying) {
        newProps[field] = targetUnderlying[field];
      }
    }
  }
  return createOsdkObject(sourceValue[ClientRef], objectDef, newProps);
}
//# sourceMappingURL=rdpFieldOperations.js.map