#!/bin/bash

# Revert all kw() usages back to choice() patterns
# This script reverts the changes made when attempting to use kw() helper

# Make a backup first
cp grammar.js grammar.js.kw_backup

# List of all keywords that need reverting
# Format: keyword|CasedVariants
keywords=(
  "namespace|'namespace', 'Namespace', 'NAMESPACE'"
  "table|'table', 'Table', 'TABLE'"
  "codeunit|'codeunit', 'Codeunit', 'CODEUNIT'"
  "page|'page', 'Page', 'PAGE'"
  "report|'report', 'Report', 'REPORT'"
  "query|'query', 'Query', 'QUERY'"
  "xmlport|'xmlport', 'XMLport', 'XMLPORT'"
  "enum|'enum', 'Enum', 'ENUM'"
  "interface|'interface', 'Interface', 'INTERFACE'"
  "controladdin|'controladdin', 'ControlAddIn', 'CONTROLADDIN'"
  "dotnet|'dotnet', 'DotNet', 'DOTNET'"
  "permissionset|'permissionset', 'PermissionSet', 'PERMISSIONSET'"
  "entitlement|'entitlement', 'Entitlement', 'ENTITLEMENT'"
  "profile|'profile', 'Profile', 'PROFILE'"
  "pagecustomization|'pagecustomization', 'PageCustomization', 'PAGECUSTOMIZATION'"
  "tableextension|'tableextension', 'TableExtension', 'TABLEEXTENSION'"
  "pageextension|'pageextension', 'PageExtension', 'PAGEEXTENSION'"
  "enumextension|'enumextension', 'EnumExtension', 'ENUMEXTENSION'"
  "permissionsetextension|'permissionsetextension', 'PermissionSetExtension', 'PERMISSIONSETEXTENSION'"
  "implements|'implements', 'Implements', 'IMPLEMENTS'"
  "extends|'extends', 'Extends', 'EXTENDS'"
  "customizes|'customizes', 'Customizes', 'CUSTOMIZES'"
  "assembly|'assembly', 'Assembly', 'ASSEMBLY'"
  "type|'type', 'Type', 'TYPE'"
  "value|'value', 'Value', 'VALUE'"
  "procedure|'procedure', 'Procedure', 'PROCEDURE'"
  "record|'record', 'Record', 'RECORD'"
  "temporary|'temporary', 'Temporary', 'TEMPORARY'"
  "caption|'caption', 'Caption', 'CAPTION'"
  "captionml|'captionml', 'CaptionML', 'CAPTIONML'"
  "description|'description', 'Description', 'DESCRIPTION'"
  "enabled|'enabled', 'Enabled', 'ENABLED'"
  "visible|'visible', 'Visible', 'VISIBLE'"
  "sourcetable|'sourcetable', 'SourceTable', 'SOURCETABLE'"
  "sourcetableview|'sourcetableview', 'SourceTableView', 'SOURCETABLEVIEW'"
  "importance|'importance', 'Importance', 'IMPORTANCE'"
  "tooltip|'tooltip', 'ToolTip', 'TOOLTIP'"
  "tooltipml|'tooltipml', 'ToolTipML', 'TOOLTIPML'"
  "showcaption|'showcaption', 'ShowCaption', 'SHOWCAPTION'"
  "extendeddatatype|'extendeddatatype', 'ExtendedDataType', 'EXTENDEDDATATYPE'"
  "datacaptionfields|'datacaptionfields', 'DataCaptionFields', 'DATACAPTIONFIELDS'"
  "extensible|'extensible', 'Extensible', 'EXTENSIBLE'"
  "optioncaptionml|'optioncaptionml', 'OptionCaptionML', 'OPTIONCAPTIONML'"
  "captionclass|'captionclass', 'CaptionClass', 'CAPTIONCLASS'"
  "excludecaption|'excludecaption', 'ExcludeCaption', 'EXCLUDECAPTION'"
  "includecaption|'includecaption', 'IncludeCaption', 'INCLUDECAPTION'"
  "datapercompany|'datapercompany', 'DataPerCompany', 'DATAPERCOMPANY'"
  "replicatedata|'replicatedata', 'ReplicateData', 'REPLICATEDATA'"
  "assignmentcompatibility|'assignmentcompatibility', 'AssignmentCompatibility', 'ASSIGNMENTCOMPATIBILITY'"
  "columnstoreindex|'columnstoreindex', 'ColumnStoreIndex', 'COLUMNSTOREINDEX'"
  "compressiontype|'compressiontype', 'CompressionType', 'COMPRESSIONTYPE'"
  "inherententitlements|'inherententitlements', 'InherentEntitlements', 'INHERENTENTITLEMENTS'"
  "inherentpermissions|'inherentpermissions', 'InherentPermissions', 'INHERENTPERMISSIONS'"
  "optimizefortextsearch|'optimizefortextsearch', 'OptimizeForTextSearch', 'OPTIMIZEFORTEXTSEARCH'"
  "label|'label', 'Label', 'LABEL'"
  "option|'option', 'Option', 'OPTION'"
  "integer|'integer', 'Integer', 'INTEGER'"
  "decimal|'decimal', 'Decimal', 'DECIMAL'"
  "boolean|'boolean', 'Boolean', 'BOOLEAN'"
  "text|'text', 'Text', 'TEXT'"
  "code|'code', 'Code', 'CODE'"
  "date|'date', 'Date', 'DATE'"
  "time|'time', 'Time', 'TIME'"
  "datetime|'datetime', 'DateTime', 'DATETIME'"
  "blob|'blob', 'BLOB', 'Blob'"
  "byte|'byte', 'Byte', 'BYTE'"
  "char|'char', 'Char', 'CHAR'"
  "duration|'duration', 'Duration', 'DURATION'"
  "dateformula|'dateformula', 'DateFormula', 'DATEFORMULA'"
  "guid|'guid', 'Guid', 'GUID'"
  "recordid|'recordid', 'RecordId', 'RECORDID'"
  "variant|'variant', 'Variant', 'VARIANT'"
  "dialog|'dialog', 'Dialog', 'DIALOG'"
  "action|'action', 'Action', 'ACTION'"
  "filterpagebuilder|'filterpagebuilder', 'FilterPageBuilder', 'FILTERPAGEBUILDER'"
  "jsontoken|'jsontoken', 'JsonToken', 'JSONTOKEN'"
  "jsonvalue|'jsonvalue', 'JsonValue', 'JSONVALUE'"
  "jsonarray|'jsonarray', 'JsonArray', 'JSONARRAY'"
  "jsonobject|'jsonobject', 'JsonObject', 'JSONOBJECT'"
  "media|'media', 'Media', 'MEDIA'"
  "mediaset|'mediaset', 'MediaSet', 'MEDIASET'"
  "ostream|'ostream', 'OStream', 'OSTREAM'"
  "instream|'instream', 'InStream', 'INSTREAM'"
  "outstream|'outstream', 'OutStream', 'OUTSTREAM'"
  "secrettext|'secrettext', 'SecretText', 'SECRETTEXT'"
  "moduleinfo|'moduleinfo', 'ModuleInfo', 'MODULEINFO'"
  "objecttype|'objecttype', 'ObjectType', 'OBJECTTYPE'"
  "keyref|'keyref', 'KeyRef', 'KEYREF'"
  "xmldocument|'xmldocument', 'XmlDocument', 'XMLDOCUMENT'"
  "xmlnode|'xmlnode', 'XmlNode', 'XMLNODE'"
  "xmlelement|'xmlelement', 'XmlElement', 'XMLELEMENT'"
  "xmlnodelist|'xmlnodelist', 'XmlNodeList', 'XMLNODELIST'"
  "xmlattribute|'xmlattribute', 'XmlAttribute', 'XMLATTRIBUTE'"
  "xmlattributecollection|'xmlattributecollection', 'XmlAttributeCollection', 'XMLATTRIBUTECOLLECTION'"
  "testpage|'testpage', 'TestPage', 'TESTPAGE'"
  "testrequestpage|'testrequestpage', 'TestRequestPage', 'TESTREQUESTPAGE'"
  "fields|'fields', 'Fields', 'FIELDS'"
  "keys|'keys', 'Keys', 'KEYS'"
  "view|'view', 'View', 'VIEW'"
  "trigger|'trigger', 'Trigger', 'TRIGGER'"
  "subtype|'subtype', 'SubType', 'SUBTYPE'"
  "drilldownpageid|'drilldownpageid', 'DrillDownPageId', 'DRILLDOWNPAGEID'"
  "lookuppageid|'lookuppageid', 'LookupPageId', 'LOOKUPPAGEID'"
  "system|'system', 'System', 'SYSTEM'"
  "onrun|'onrun', 'OnRun', 'ONRUN'"
  "oninsert|'oninsert', 'OnInsert', 'ONINSERT'"
  "onmodify|'onmodify', 'OnModify', 'ONMODIFY'"
  "ondelete|'ondelete', 'OnDelete', 'ONDELETE'"
  "onrename|'onrename', 'OnRename', 'ONRENAME'"
  "onvalidate|'onvalidate', 'OnValidate', 'ONVALIDATE'"
  "onaftergetrecord|'onaftergetrecord', 'OnAfterGetRecord', 'ONAFTERGETRECORD'"
  "onafterinsertevent|'onafterinsertevent', 'OnAfterInsertEvent', 'ONAFTERINSERTEVENT'"
  "onaftermodifyevent|'onaftermodifyevent', 'OnAfterModifyEvent', 'ONAFTERMODIFYEVENT'"
  "onafterdeleteevent|'onafterdeleteevent', 'OnAfterDeleteEvent', 'ONAFTERDELETEEVENT'"
  "onaftervalidate|'onaftervalidate', 'OnAfterValidate', 'ONAFTERVALIDATE'"
  "onlookup|'onlookup', 'OnLookup', 'ONLOOKUP'"
  "onassistedit|'onassistedit', 'OnAssistEdit', 'ONASSISTEDIT'"
  "ondrilldown|'ondrilldown', 'OnDrillDown', 'ONDRILLDOWN'"
  "disabled|'disabled', 'Disabled', 'DISABLED'"
  "nonrestrictive|'nonrestrictive', 'NonRestrictive', 'NONRESTRICTIVE'"
  "restrictive|'restrictive', 'Restrictive', 'RESTRICTIVE'"
  "while|'while', 'WHILE', 'While'"
  "with|'with', 'WITH', 'With'"
  "for|'for', 'FOR', 'For'"
  "foreach|'foreach', 'FOREACH', 'Foreach'"
  "repeat|'repeat', 'REPEAT', 'Repeat'"
  "do|'do', 'DO', 'Do'"
  "until|'until', 'UNTIL', 'Until'"
  "in|'in', 'IN', 'In'"
  "to|'to', 'TO', 'To'"
  "downto|'downto', 'DOWNTO', 'Downto'"
  "is|'is', 'IS', 'Is'"
  "as|'as', 'AS', 'As'"
  "not|'not', 'NOT', 'Not'"
  "and|'and', 'AND', 'And'"
  "or|'or', 'OR', 'Or'"
  "xor|'xor', 'XOR', 'Xor'"
  "if|'if', 'IF', 'If'"
  "then|'then', 'THEN', 'Then'"
  "else|'else', 'ELSE', 'Else'"
  "case|'case', 'CASE', 'Case'"
  "of|'of', 'OF', 'Of'"
  "end|'end', 'END', 'End'"
  "database|'database', 'DATABASE', 'Database'"
  "const|'const', 'CONST', 'Const'"
  "field|'field', 'FIELD', 'Field'"
  "lookup|'lookup', 'LOOKUP', 'Lookup'"
  "where|'where', 'WHERE', 'Where'"
  "upperlimit|'upperlimit', 'UPPERLIMIT', 'Upperlimit'"
  "filter|'filter', 'FILTER', 'Filter'"
  "count|'count', 'COUNT', 'Count'"
  "sum|'sum', 'SUM', 'Sum'"
  "average|'average', 'AVERAGE', 'Average'"
  "min|'min', 'MIN', 'Min'"
  "max|'max', 'MAX', 'Max'"
  "exist|'exist', 'EXIST', 'Exist'"
  "comment|'comment', 'Comment', 'COMMENT'"
  "never|'never', 'Never', 'NEVER'"
)

echo "Reverting kw() usages..."

for entry in "${keywords[@]}"; do
  keyword=$(echo "$entry" | cut -d'|' -f1)
  variants=$(echo "$entry" | cut -d'|' -f2)
  
  # Replace kw('keyword') with choice(variants)
  sed -i "s/kw('$keyword')/choice($variants)/g" grammar.js
done

echo "Revert complete. Check the grammar.js file and run tests."