{"version":3,"file":"conditions.mjs","sources":["../../../admin/src/utils/conditions.ts"],"sourcesContent":["import type {\n  AnyAttribute,\n  AttributeConditionValue,\n  AttributeConditions,\n  ContentTypes,\n} from '../types';\n\ninterface DependentRow {\n  contentTypeUid: string;\n  contentType: string;\n  attribute: string;\n}\n\ntype VisibleConditionEntry = {\n  operator: string;\n  fieldVar: { var: string };\n  value: AttributeConditionValue;\n};\n\nconst isRecord = (value: unknown): value is Record<string, unknown> =>\n  typeof value === 'object' && value !== null && !Array.isArray(value);\n\nconst isFieldVar = (value: unknown): value is { var: string } =>\n  isRecord(value) && typeof value.var === 'string';\n\nconst isAttributeConditionValue = (value: unknown): value is AttributeConditionValue =>\n  typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean';\n\nexport const getVisibleConditionEntries = (\n  condition?: AttributeConditions | null\n): VisibleConditionEntry[] => {\n  const visible = condition?.visible;\n\n  if (!isRecord(visible)) {\n    return [];\n  }\n\n  return Object.entries(visible).reduce<VisibleConditionEntry[]>((entries, [operator, rule]) => {\n    if (!Array.isArray(rule)) {\n      return entries;\n    }\n\n    const [fieldVar, value] = rule;\n\n    if (!isFieldVar(fieldVar) || !isAttributeConditionValue(value)) {\n      return entries;\n    }\n\n    entries.push({ operator, fieldVar, value });\n\n    return entries;\n  }, []);\n};\n\nexport const getFirstVisibleConditionEntry = (condition?: AttributeConditions | null) =>\n  getVisibleConditionEntries(condition)[0] ?? null;\n\nexport const checkDependentRows = (\n  contentTypes: ContentTypes,\n  fieldName: string\n): DependentRow[] => {\n  const dependentRows: DependentRow[] = [];\n\n  Object.entries(contentTypes).forEach(([contentTypeUid, contentType]) => {\n    if (contentType.attributes !== undefined) {\n      // Handle both array and object formats of attributes\n      const attributes = Array.isArray(contentType.attributes)\n        ? contentType.attributes.reduce<Record<string, AnyAttribute>>((acc, attr, index) => {\n            acc[index.toString()] = attr;\n            return acc;\n          }, {})\n        : contentType.attributes;\n\n      Object.entries(attributes).forEach(([attrName, attr]) => {\n        getVisibleConditionEntries(attr.conditions).forEach(({ fieldVar }) => {\n          // Check if this condition references our field\n          if (fieldVar.var === fieldName) {\n            dependentRows.push({\n              contentTypeUid,\n              contentType: contentType.info.displayName,\n              attribute: attr.name || attrName,\n            });\n          }\n        });\n      });\n    }\n  });\n  return dependentRows;\n};\n\nexport const formatCondition = (\n  condition: AttributeConditions,\n  availableFields: Array<{ name: string; type: string }>,\n  attributeName: string\n): string => {\n  const conditionEntry = getFirstVisibleConditionEntry(condition);\n\n  if (conditionEntry === null) {\n    return '';\n  }\n\n  const { operator, fieldVar, value } = conditionEntry;\n\n  const dependsOnField = availableFields.find((field) => field.name === fieldVar.var);\n  const dependsOnFieldName = dependsOnField ? dependsOnField.name : fieldVar.var;\n\n  const operatorText = operator === '==' ? 'is' : 'is not';\n  const valueText = String(value);\n  const actionText = operator === '==' ? 'Show' : 'Hide';\n\n  return `If ${dependsOnFieldName} ${operatorText} ${valueText}, then ${actionText} ${attributeName}`;\n};\n\nexport const getAvailableConditionFields = (\n  attributes: AnyAttribute[],\n  currentFieldName: string\n) => {\n  return attributes\n    .filter((attr) => {\n      // Only include boolean and enum fields\n      const isCorrectType = attr.type === 'boolean' || attr.type === 'enumeration';\n      // Exclude the current field to prevent self-referential conditions\n      const isNotCurrentField = attr.name !== currentFieldName;\n      return isCorrectType && isNotCurrentField;\n    })\n    .map((attr) => ({\n      name: attr.name,\n      type: attr.type,\n      enum: attr.type === 'enumeration' ? attr.enum : undefined,\n    }));\n};\n"],"names":["isRecord","value","Array","isArray","isFieldVar","var","isAttributeConditionValue","getVisibleConditionEntries","condition","visible","Object","entries","reduce","operator","rule","fieldVar","push","getFirstVisibleConditionEntry","checkDependentRows","contentTypes","fieldName","dependentRows","forEach","contentTypeUid","contentType","attributes","undefined","acc","attr","index","toString","attrName","conditions","info","displayName","attribute","name","getAvailableConditionFields","currentFieldName","filter","isCorrectType","type","isNotCurrentField","map","enum"],"mappings":"AAmBA,MAAMA,QAAAA,GAAW,CAACC,KAAA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