/*
 * 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 { useOsdkMetadata } from "@osdk/react";
import { useMemo } from "react";
import { renderDefaultCell } from "../DefaultCellRenderer.js";
import { shouldShowEditableCell } from "../utils/shouldShowEditableCell.js";
/**
 * Hook which builds column definitions for tanstack-table given the objectSet
 */
export function useColumnDefs(objectType, columnDefinitions) {
  const {
    metadata,
    loading,
    error
  } = useOsdkMetadata(objectType);
  const columns = useMemo(() => {
    const objectProperties = metadata?.properties;
    // If columnDefinitions is provided, construct colDefs with it
    if (columnDefinitions) {
      return getColumnsFromColumnDefinitions(columnDefinitions, objectProperties);
    }

    // If not, return the default columns from the object properties
    return getDefaultColumns(objectProperties);
  }, [columnDefinitions, metadata?.properties]);
  return {
    columns,
    loading,
    error
  };
}
function getColumnsFromColumnDefinitions(columnDefinitions, objectProperties) {
  return columnDefinitions.map(col => {
    const {
      locator,
      width,
      minWidth,
      maxWidth,
      resizable,
      orderable,
      editable,
      renderCell,
      renderHeader,
      columnName
    } = col;
    const editFieldConfig = col.editable ? col.editFieldConfig : undefined;
    const validateEdit = col.editable ? col.validateEdit : undefined;
    const propertyMetadata = locator.type === "property" ? objectProperties?.[locator.id] : undefined;
    const colKey = locator.id;
    const dataType = propertyMetadata?.type && typeof propertyMetadata.type === "string" ? propertyMetadata.type : undefined;
    const markingType = propertyMetadata?.typeMetadata?.type === "marking" ? propertyMetadata.typeMetadata.markingType : undefined;
    const colDef = {
      id: colKey,
      accessorKey: colKey,
      header: renderHeader ?? (columnName || propertyMetadata?.displayName),
      meta: {
        columnName: columnName || propertyMetadata?.displayName,
        isAsyncColumn: locator.type === "function",
        isVisible: col.isVisible !== false,
        editable,
        editFieldConfig,
        dataType,
        markingType,
        validateEdit
      },
      size: width,
      ...(minWidth ? {
        minSize: minWidth
      } : {}),
      ...(maxWidth ? {
        maxSize: maxWidth
      } : {}),
      enableResizing: resizable,
      // Function-backed columns must be sorted on the frontend, so disable sorting for now
      enableSorting: locator.type === "function" ? false : orderable,
      cell: cellContext => {
        const object = cellContext.row.original;
        const meta = cellContext.table.options.meta;
        const isEditable = shouldShowEditableCell(editable, meta?.onCellEdit, meta?.isInEditMode);
        if (renderCell && !isEditable) {
          return renderCell(object, locator);
        }
        return renderDefaultCell(cellContext);
      }
    };
    return colDef;
  });
}
function getDefaultColumns(objectProperties) {
  if (!objectProperties) return [];
  return Object.entries(objectProperties).map(([key, property]) => {
    const colDef = {
      accessorKey: key,
      header: property.displayName ?? key
    };
    return colDef;
  });
}
//# sourceMappingURL=useColumnDefs.js.map