import type { Condition } from "@arco/clarizen-lib";

export type EntityFilter =
  | { kind: "all" }
  | {
      kind: "state";
      fieldName: string;
      stateValue: string;
      stateLabel: string;
    }
  | { kind: "czql"; whereClause: string };

export function formatFilterSummary(filter: EntityFilter): string {
  switch (filter.kind) {
    case "all":
      return "Todos";
    case "state":
      return `${filter.fieldName} = ${filter.stateLabel}`;
    case "czql":
      return `WHERE ${filter.whereClause}`;
  }
}

export function buildWhereCondition(filter: EntityFilter): Condition | undefined {
  switch (filter.kind) {
    case "all":
      return undefined;
    case "state":
      return {
        leftExpression: { fieldName: filter.fieldName },
        operator: "Equal",
        rightExpression: { value: filter.stateValue },
      };
    case "czql":
      return { text: filter.whereClause };
  }
}
