import type {
  ConditionLogic,
  ConditionParam,
  ConditionParamV1,
} from "@/definitions/shared/conditions"
import type { ResponsiveColumns, ResponsiveColumnsV1 } from "@/definitions/shared/responsive"

/**
 * v1 condition operator → v2 logic string.
 *
 * The v1 docs only documented `"equals"`; this table also covers
 * common variants that show up in the wild (`"=="`, `"!="`, `"gt"`,
 * `"lt"`, `"contains"`, ...). Unknown operators pass through verbatim
 * — useful for forward-compat and easier to debug than a silent fallback.
 */
const V1_OPERATOR_TO_V2_LOGIC: Record<string, ConditionLogic> = {
  equals: "==",
  "=": "==",
  "==": "==",
  "===": "==",
  not_equals: "!=",
  "!=": "!=",
  "!==": "!=",
  greater_than: ">",
  gt: ">",
  ">": ">",
  greater_than_or_equal: ">=",
  gte: ">=",
  ">=": ">=",
  less_than: "<",
  lt: "<",
  "<": "<",
  less_than_or_equal: "<=",
  lte: "<=",
  "<=": "<=",
  in: "in-array",
  in_array: "in-array",
  "in-array": "in-array",
  not_in: "not-in-array",
  not_in_array: "not-in-array",
  "not-in-array": "not-in-array",
  contains: "contains",
  not_contains: "not-contains",
  "not-contains": "not-contains",
  starts_with: "starts-with",
  "starts-with": "starts-with",
  ends_with: "ends-with",
  "ends-with": "ends-with",
  matches: "matches",
  regex: "matches",
  is_truthy: "is-truthy",
  truthy: "is-truthy",
  is_falsy: "is-falsy",
  falsy: "is-falsy",
  is_null: "is-null",
  is_not_null: "is-not-null",
  is_odd: "is-odd",
  is_even: "is-even",
  is_empty: "zero-length",
  zero_length: "zero-length",
  has_items: "g0length",
  length_eq: "length-eq-value",
  length_gt: "length-gt-value",
  length_lt: "length-lt-value",
}

const V2_LOGIC_TO_V1_OPERATOR: Record<ConditionLogic, string> = {
  "==": "equals",
  "!=": "not_equals",
  ">": "greater_than",
  ">=": "greater_than_or_equal",
  "<": "less_than",
  "<=": "less_than_or_equal",
  "in-array": "in",
  "not-in-array": "not_in",
  "starts-with": "starts_with",
  "ends-with": "ends_with",
  contains: "contains",
  "not-contains": "not_contains",
  matches: "matches",
  "is-odd": "is_odd",
  "is-even": "is_even",
  "is-truthy": "is_truthy",
  "is-falsy": "is_falsy",
  "is-null": "is_null",
  "is-not-null": "is_not_null",
  g0length: "has_items",
  n0length: "is_empty",
  "length-eq-value": "length_eq",
  "length-gt-value": "length_gt",
  "length-lt-value": "length_lt",
  "zero-length": "is_empty",
}

/** v1 `{field, operator, value}` → v2 `{name, logic, value}`. */
export function v1ConditionParamToV2(
  param: ConditionParamV1,
): ConditionParam {
  return {
    name: param.field,
    logic: (V1_OPERATOR_TO_V2_LOGIC[param.operator] ?? param.operator) as ConditionLogic,
    value: param.value,
  }
}

/** v2 `{name, logic, value}` → v1 `{field, operator, value}`. */
export function v2ConditionParamToV1(
  param: ConditionParam,
): ConditionParamV1 {
  return {
    field: param.name,
    operator: V2_LOGIC_TO_V1_OPERATOR[param.logic] ?? String(param.logic),
    value: param.value,
  }
}

/**
 * Convert a v1 conditionParams array (or undefined) to a v2 array.
 * Returns undefined when the input is empty so the converted JSON
 * doesn't carry a useless empty array.
 */
export function v1ConditionParamsToV2(
  params: ConditionParamV1[] | undefined,
): ConditionParam[] | undefined {
  if (!params || params.length === 0) {
    return undefined
  }
  return params.map(v1ConditionParamToV2)
}

/** v1 `cols/colsSm/colsMd/...` → v2 `columns/columnsSm/columnsMd/...`. */
export function v1ColumnsToV2(input: ResponsiveColumnsV1): ResponsiveColumns {
  const out: ResponsiveColumns = {}
  if (input.cols !== undefined) {
    out.columns = input.cols
  }
  if (input.colsSm !== undefined) {
    out.columnsSm = input.colsSm
  }
  if (input.colsMd !== undefined) {
    out.columnsMd = input.colsMd
  }
  if (input.colsLg !== undefined) {
    out.columnsLg = input.colsLg
  }
  if (input.colsXl !== undefined) {
    out.columnsXl = input.colsXl
  }
  if (input.colsXxl !== undefined) {
    out.columnsXxl = input.colsXxl
  }
  return out
}

/** v2 → v1 reverse for the columns shape. */
export function v2ColumnsToV1(input: ResponsiveColumns): ResponsiveColumnsV1 {
  const out: ResponsiveColumnsV1 = {}
  if (input.columns !== undefined) {
    out.cols = input.columns
  }
  if (input.columnsSm !== undefined) {
    out.colsSm = input.columnsSm
  }
  if (input.columnsMd !== undefined) {
    out.colsMd = input.columnsMd
  }
  if (input.columnsLg !== undefined) {
    out.colsLg = input.columnsLg
  }
  if (input.columnsXl !== undefined) {
    out.colsXl = input.columnsXl
  }
  if (input.columnsXxl !== undefined) {
    out.colsXxl = input.columnsXxl
  }
  return out
}

export const __testing = {
  V1_OPERATOR_TO_V2_LOGIC,
  V2_LOGIC_TO_V1_OPERATOR,
}
