/*
 * Copyright 2024 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 invariant from "tiny-invariant";
import { modernToLegacyWhereClause } from "../internal/conversions/modernToLegacyWhereClause.js";
import { derivedPropertyDefinitionFactory } from "./derivedPropertyDefinitionFactory.js";
/** @internal */
export function createWithPropertiesObjectSet(objectType, objectSet, definitionMap, fromBaseObjectSet = false) {
  return {
    pivotTo: link => {
      return createWithPropertiesObjectSet(objectType, {
        type: "searchAround",
        objectSet,
        link
      }, definitionMap);
    },
    where: clause => {
      const rdpNames = new Set(definitionMap.keys());
      return createWithPropertiesObjectSet(objectType, {
        type: "filter",
        objectSet: objectSet,
        where: modernToLegacyWhereClause(clause, objectType, rdpNames)
      }, definitionMap);
    },
    aggregate: (aggregation, opt) => {
      const splitAggregation = aggregation.split(":");
      !(splitAggregation.length === 2 || splitAggregation[0] === "$count") ? process.env.NODE_ENV !== "production" ? invariant(false, "Invalid aggregation format") : invariant(false) : void 0;
      const [aggregationPropertyName, aggregationOperation] = splitAggregation;
      let aggregationOperationDefinition;
      switch (aggregationOperation) {
        case "sum":
        case "avg":
        case "min":
        case "max":
        case "exactDistinct":
        case "approximateDistinct":
          aggregationOperationDefinition = {
            type: aggregationOperation,
            selectedPropertyApiName: aggregationPropertyName
          };
          break;
        case "approximatePercentile":
          aggregationOperationDefinition = {
            type: "approximatePercentile",
            selectedPropertyApiName: aggregationPropertyName,
            approximatePercentile: opt?.percentile ?? .5
          };
          break;
        case "collectSet":
        case "collectList":
          aggregationOperationDefinition = {
            type: aggregationOperation,
            selectedPropertyApiName: aggregationPropertyName,
            limit: opt?.limit ?? 100
          };
          break;
        case undefined:
          if (aggregationPropertyName === "$count") {
            aggregationOperationDefinition = {
              type: "count"
            };
            break;
          }
        default:
          process.env.NODE_ENV !== "production" ? invariant(false, "Invalid aggregation operation " + aggregationOperation) : invariant(false);
      }
      const wrappedObjectSet = {
        type: "selection",
        objectSet: objectSet,
        operation: aggregationOperationDefinition
      };
      const selectorResult = derivedPropertyDefinitionFactory(wrappedObjectSet, definitionMap);
      definitionMap.set(selectorResult, wrappedObjectSet);
      return selectorResult;
    },
    selectProperty: name => {
      if (fromBaseObjectSet) {
        const wrappedObjectSet = {
          type: "property",
          apiName: name
        };
        const selectorResult = derivedPropertyDefinitionFactory(wrappedObjectSet, definitionMap);
        definitionMap.set(selectorResult, wrappedObjectSet);
        return selectorResult;
      }
      const wrappedObjectSet = {
        type: "selection",
        objectSet: objectSet,
        operation: {
          type: "get",
          selectedPropertyApiName: name
        }
      };
      const selectorResult = derivedPropertyDefinitionFactory(wrappedObjectSet, definitionMap);
      definitionMap.set(selectorResult, wrappedObjectSet);
      return selectorResult;
    },
    constant: {
      double: () => {
        process.env.NODE_ENV !== "production" ? invariant(false, "Not supported") : invariant(false);
      },
      integer: () => {
        process.env.NODE_ENV !== "production" ? invariant(false, "Not supported") : invariant(false);
      },
      long: () => {
        process.env.NODE_ENV !== "production" ? invariant(false, "Not supported") : invariant(false);
      },
      datetime: () => {
        process.env.NODE_ENV !== "production" ? invariant(false, "Not supported") : invariant(false);
      },
      timestamp: () => {
        process.env.NODE_ENV !== "production" ? invariant(false, "Not supported") : invariant(false);
      }
    }
  };
}
//# sourceMappingURL=createWithPropertiesObjectSet.js.map