/*
 * 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 { useObjectSet, useOsdkObjects } from "@osdk/react";
import { useMemo } from "react";
import { DEFAULT_OBJECT_TABLE_DEDUPE_INTERVAL_MS, DEFAULT_PAGE_SIZE } from "../utils/constants.js";
import { useFunctionColumnsData } from "./useFunctionColumnsData.js";
/**
 * This hook is a wrapper that conditionally uses either useObjectSet or useOsdkObjects
 * based on whether an objectSet prop is provided.
 * It extracts RDP locators from columnDefinitions and applies withProperties
 * to return data containing the derived properties.
 */
export function useObjectTableData({
  objectOrInterfaceType,
  columnDefinitions,
  filter,
  sorting,
  objectSet,
  objectSetOptions,
  dedupeIntervalMs = DEFAULT_OBJECT_TABLE_DEDUPE_INTERVAL_MS,
  pageSize = DEFAULT_PAGE_SIZE,
  streamUpdates
}) {
  const orderBy = useMemo(() => {
    if (!sorting || sorting.length === 0) {
      return undefined;
    }
    return sorting.reduce((acc, sort) => {
      acc[sort.id] = sort.desc ? "desc" : "asc";
      return acc;
    }, {});
  }, [sorting]);

  // Extract derived properties definition
  const withProperties = useMemo(() => {
    if (!columnDefinitions) {
      return;
    }
    const rdpColumns = columnDefinitions.map(colDef => colDef.locator).filter(colLocator => {
      return colLocator.type === "rdp";
    });
    if (!rdpColumns.length) {
      return;
    }
    return rdpColumns.reduce((acc, cur) => {
      return {
        ...acc,
        [cur.id]: cur.creator
      };
    }, {});
  }, [columnDefinitions]);

  // Use the caller's objectSet whenever provided (object or interface types), else fetch
  // by type. For an interface objectSet, rows carry interface-declared props (+ withProperties)
  // only, not the full underlying object — see the objectSet prop docs.
  const shouldUseObjectSet = !!objectSet;
  const objectSetResult = useObjectSet(shouldUseObjectSet ? objectSet : undefined, {
    ...objectSetOptions,
    withProperties: withProperties,
    where: filter,
    orderBy,
    pageSize,
    enabled: shouldUseObjectSet,
    dedupeIntervalMs,
    streamUpdates
  });
  const osdkObjectsResult = useOsdkObjects(objectOrInterfaceType, {
    withProperties,
    pageSize,
    where: filter,
    orderBy,
    enabled: !shouldUseObjectSet,
    dedupeIntervalMs,
    streamUpdates
  });

  // Get the result from the appropriate hook
  const baseResult = shouldUseObjectSet ? objectSetResult : osdkObjectsResult;

  // Call useFunctionColumnsData to get function column data
  const functionColumnData = useFunctionColumnsData({
    objectOrInterfaceType,
    objects: baseResult.data,
    columnDefinitions,
    pageSize
  });

  // Merge function column data into each object
  const mergedData = useMemo(() => {
    if (!baseResult.data) return baseResult.data;
    return baseResult.data.map(obj => {
      const objKey = String(obj.$primaryKey);
      const functionData = {};

      // Collect all function column data for this object
      Object.entries(functionColumnData).forEach(([columnId, columnData]) => {
        if (columnData[objKey]) {
          functionData[columnId] = columnData[objKey];
        }
      });

      // Return object with function data merged in
      return {
        ...obj,
        ...functionData
      };
    });
  }, [baseResult.data, functionColumnData]);

  // Return the result with merged data
  return {
    ...baseResult,
    data: mergedData
  };
}
//# sourceMappingURL=useObjectTableData.js.map