All files / src/display Chat.ts

91.45% Statements 1990/2176
89.58% Branches 284/317
91.42% Functions 32/35
91.45% Lines 1990/2176

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177125x 180x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x                                       125x 154x 154x 154x 154x                                                                                                                                                             125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 5630x 5630x 36x 36x 4x 32x 5594x 125x 29x 29x 13x 29x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 2921x 2921x 2921x 4x 125x 7062x 7062x 125x 125x 383x 383x 383x 383x 383x 333x 333x 333x 333x 333x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 1384x 1384x 1384x 121x 121x 121x 121x 121x 1384x 1413x 1413x 1384x 125x 125x 1413x 122x 122x 125x 125x 125x 125x 125x 125x 125x 122x 122x 122x 122x 122x 125x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 125x 4x 4x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 136x 136x 136x 136x 136x 125x 136x 136x 136x 136x 136x 136x 136x 136x 125x 268x 268x 266x 266x 268x 268x 268x 268x 268x 268x 268x 268x 268x 268x 268x 268x 255x 255x 255x 255x 1x 1x 255x 255x 3296x 3296x 3296x 3296x 3296x 3296x     3296x 3296x 1092x 1092x 1092x 255x 255x 166x 255x 88x 88x 88x 88x 88x 88x 255x 5x 255x 30x 30x 88x 88x 88x 88x 88x 88x 254x 30x 254x 255x   255x 88x 81x 81x   81x 81x 26x 26x 81x     81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 88x 268x 268x 268x 268x 268x 268x 145x 125x 125x 125x 125x 125x 125x 125x 125x 9x 9x 9x 9x 66x 66x               66x 63x 9x 9x 9x 9x 9x 9x 9x 9x 125x 3383x 3383x 3383x 3383x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 23x 23x 23x 23x 20x 20x 20x 20x 20x 20x 23x 23x 23x 23x 23x 23x 23x 23x 2x 23x 125x 125x 125x 125x 125x 125x 6x               125x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 125x 10x 10x 10x 10x 10x 10x 10x 10x 11x 11x 11x 11x 11x 11x 11x 11x 11x 10x 10x 10x 10x 10x 10x 10x 125x 125x 30x 30x 30x 30x 30x 30x 30x 31x 31x 31x 31x 31x 28x 28x 28x 31x 31x 1x 1x 125x 125x 125x 125x 125x 125x 30x 30x 30x 29x 29x 1x 1x 1x 1x 1x 1x 1x 125x 3383x 125x 1884x 118x 118x 1884x 1766x 1766x 1766x 1766x 1882x 845x 845x 1884x 1884x 1184x 1884x 720x 720x 1884x 1884x 1884x 1884x 1884x 136x 136x 136x 136x 1882x 1630x 1630x 1858x 1251x 1251x 1251x 1828x 4x 4x 4x 1884x 1x 1849x 125x 118x 118x 67x 118x 118x 118x 118x 801x 749x 749x 749x                       708x 705x 682x 682x 682x 682x 682x 682x 801x 26x 26x 26x 26x 26x 26x 26x 92x 67x 67x 67x 118x 118x 125x 118x 118x 118x 118x 1176x 1176x 1176x 1176x 1176x 801x 801x 801x 801x 1176x 1176x 1176x 1176x 1176x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 247x 247x 247x 247x 247x 125x 5x 5x 5x 5x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 5x 5x 5x 4x 4x 4x 5x 125x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x                                                                               125x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x     4x 4x 20x 20x 20x 20x 20x 4x 125x   2098x 2098x 2098x 1703x 1703x 2098x 395x 395x 395x 125x 2098x 2098x 2098x 2098x 2098x 2098x 2098x 2098x 2098x 2098x 2098x 2098x 39x 2098x 2098x 2098x 630x 2098x 2098x 2098x 2098x 2098x 2098x 639x 639x 639x 639x 639x 639x 2098x 2098x 2080x 2098x 639x 11x 11x 1472x 1472x 1472x 1472x 1472x 1472x 1472x 3103x 3103x 3103x 1631x 3103x 3103x 3103x 1002x 3103x 2101x 2098x 4x 4x 4x 4x 4x 1592x 1592x 1592x 1592x 1592x 629x 629x 623x 2x 2x 2x 2x 2x 2x 5x 5x 5x 5x 5x 5x 5x 629x 6x 6x 629x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 629x                         629x 45x 2x 2x 2x 45x 45x 45x 45x 45x 45x 2101x 1472x 1472x 1472x 1472x 1472x 1472x 1472x 1472x 1472x 1472x 1472x 1472x 1472x 1472x 775x 775x 775x 775x 1472x   1472x 1472x 1318x 1318x 1318x 1472x 1472x 1472x 1472x 3x 3x 1472x 1472x 1472x 1472x 1472x 1472x 1472x 1472x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1472x 1472x 1472x 1472x 1472x 1469x 3x 1472x 1472x 1435x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1435x 1435x 1435x 1435x 1435x 1419x 16x 1435x 1435x 1435x 1435x 1435x 1435x 1435x 2098x       2098x 125x 2x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 125x 125x 125x 125x 125x 121x 121x 121x 121x 121x 121x 125x 1541x 1541x 123x 123x 123x 123x 123x 142x 142x 142x 123x 125x 125x 1538x 1538x 125x 125x 125x 125x 125x 125x 1538x 1538x 275x 275x 275x 275x 1384x 125x 125x 125x 125x 157x 157x 125x 125x 125x 275x 275x 275x 125x 125x 125x 125x 125x 125x 125x 123x 123x 123x 123x 125x 125x 125x 121x 125x 125x 301x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 1537x 275x 122x 122x 122x 122x 1537x 1537x 122x 122x 122x 122x 122x 122x 122x 122x 122x 122x 122x 122x 1501x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x  
import { TemplateResult, html, nothing, PropertyValueMap, css } from 'lit';
import { property } from 'lit/decorators.js';
import { repeat } from 'lit/directives/repeat.js';
import { RapidElement } from '../RapidElement';
import { CustomEventType, QuickReply } from '../interfaces';
import { TicketEvent } from '../events';
import { renderEventSummary } from '../events/eventRenderers';
import { DEFAULT_AVATAR } from '../webchat/assets';
import { attachmentAsString } from '../utils';
import { DateTime } from 'luxon';
 
const SCROLL_FETCH_BUFFER = 200; // pixels from top
const MIN_FETCH_TIME = 250;
 
// how long a typing indicator lives without a fresh pulse - stop events can
// be missed (they're excluded from history recovery) so indicators decay
const TYPING_TIMEOUT = 10 * 1000;
 
const getUnsendableReasonMessage = (reason: string): string => {
  switch (reason) {
    case 'no_route':
      return 'No channel available to send message';
    case 'contact_blocked':
      return 'Contact has been blocked';
    case 'contact_stopped':
      return 'Contact has been stopped';
    case 'contact_archived':
      return 'Contact is archived';
    case 'org_suspended':
      return 'Workspace is suspended';
    case 'looping':
      return 'Message loop detected';
    default:
      return 'Unable to send message';
  }
};

const getStatusReasonMessage = (reason: string): string => {
  switch (reason) {
    case 'error_limit':
      return 'Error limit reached';
    case 'too_old':
      return 'Message is too old to send';
    case 'channel_removed':
      return 'Channel was removed';
    default:
      return 'Message failed to send';
  }
};
 
export enum MessageType {
  Inline = 'inline',
  Error = 'error',
  Collapse = 'collapse',
  Note = 'note'
}

export type GroupReason =
  | 'time_elapsed'
  | 'new_author'
  | 'new_type'
  | 'initial';

export interface MessageGroup {
  messages: string[];
  reason: GroupReason;
}

export interface ObjectReference {
  uuid: string;
  name: string;
}

interface User extends ObjectReference {
  avatar?: string;
  email: string;
  first_name?: string;
  last_name?: string;
}

export interface Msg {
  text: string;
  channel: ObjectReference;
  quick_replies: (string | QuickReply)[];
  urn: string;
  direction: string;
  type: string;
  external_id?: string;
  attachments: string[];
  unsendable_reason?:
    | 'no_route'
    | 'contact_blocked'
    | 'contact_stopped'
    | 'contact_archived'
    | 'org_suspended'
    | 'looping';
}

export interface ContactEvent {
  uuid?: string;
  type: string;
  created_on: Date;
  _user?: User;
  _rendered?: { html: TemplateResult; type: MessageType };
}

export interface MsgEvent extends ContactEvent {
  msg: Msg;
  optin?: ObjectReference;
  _status?: {
    created_on: string;
    status: 'wired' | 'sent' | 'delivered' | 'read' | 'errored' | 'failed';
    reason: 'error_limit' | 'too_old' | 'channel_removed';
  };
  _deleted?: {
    created_on: string;
    by_contact: boolean;
    user?: { name: string; uuid: string };
  };
  _logs_url?: string;
}

export interface TypingEvent extends ContactEvent {
  direction?: string;
}

// typing indicators group and side with real messages: a user typing
// (outgoing) behaves like a reply, a contact typing (incoming) like a
// received message
const effectiveType = (event: ContactEvent): string => {
  if (event.type === 'typing_started') {
    return (event as TypingEvent).direction === 'incoming'
      ? 'msg_received'
      : 'msg_created';
  }
  return event.type;
};
 
// identifies who a typing indicator belongs to - the user when stamped,
// otherwise the contact
const getTypingKey = (event: TypingEvent): string => {
  return event._user?.uuid || 'contact';
};
 
// event types whose inline rendering is a self-contained pill (see
// eventRenderers.ts) — consecutive runs of these condense onto a
// single wrapping line instead of one per line. Diagnostics
// (error/warning/failure) and note bubbles stay on their own lines.
const CONDENSED_EVENT_TYPES = new Set([
  'airtime_created',
  'airtime_transferred',
  'broadcast_created',
  'call_created',
  'call_missed',
  'call_received',
  'chat_started',
  'contact_field_changed',
  'contact_groups_changed',
  'contact_language_changed',
  'contact_name_changed',
  'contact_status_changed',
  'contact_urns_changed',
  'email_created',
  'email_sent',
  'flow_entered',
  'input_labels_added',
  'optin_requested',
  'optin_started',
  'optin_stopped',
  'resthook_called',
  'run_ended',
  'run_result_changed',
  'run_started',
  'service_called',
  'session_triggered',
  'ticket_assignee_changed',
  'ticket_closed',
  'ticket_opened',
  'ticket_reopened',
  'ticket_topic_changed',
  'webhook_called'
]);
 
// whether a message has nothing to show in its bubble (and isn't deleted)
const isEmptyMsg = (event: MsgEvent): boolean =>
  !event._deleted &&
  !!event.msg &&
  !event.msg.text &&
  (event.msg.attachments || []).length === 0;
 
// local-time calendar day, used to bucket messages into day sections
const getDayKey = (date: Date): string =>
  `${date.getFullYear()}-${date.getMonth()}-${date.getDate()}`;
 
/**
 * Human label for a day section: Today, Yesterday, "Jun 22" within
 * the current year, and a full "9/1/2025" for anything older.
 * "Now" comes from luxon so it honors the same clock the rest of the
 * date rendering uses (and its test stubs).
 */
const getDayLabel = (date: Date): string => {
  const today = DateTime.now().toJSDate();
  if (getDayKey(date) === getDayKey(today)) {
    return 'Today';
  }
  const yesterday = new Date(today);
  yesterday.setDate(today.getDate() - 1);
  if (getDayKey(date) === getDayKey(yesterday)) {
    return 'Yesterday';
  }
  if (date.getFullYear() === today.getFullYear()) {
    return date.toLocaleDateString(undefined, {
      month: 'short',
      day: 'numeric'
    });
  }
  return date.toLocaleDateString();
};
 
export class Chat extends RapidElement {
  static get styles() {
    return css`
      :host {
        display: flex;
        flex-direction: column;
        flex-grow: 1;
        position: relative;
        z-index: 1;
      }
 
      slot[name='header'] {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        display: block;
      }
 
      slot[name='footer'] {
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
        display: block;
        /* The slot overlays the bottom of the chat history, so clicks
           on the chat scrollbar or messages behind it must pass
           through. Slotted footer content can opt back in with
           pointer-events: auto on its interactive bits. */
        pointer-events: none;
      }
 
      .block {
        margin-bottom: 1em;
        display: flex;
        flex-direction: row;
      }
 
      .block.outgoing {
        flex-direction: row-reverse;
      }
 
      .block.collapse {
        margin: 0;
        align-items: center;
        display: flex;
        flex-direction: column;
        margin-bottom: 0.5em;
      }
 
      .block.collapse .messsage {
        transform: scaleY(0);
        margin: 0;
        padding: 0;
        line-height: 0;
      }
 
      /* Each calendar day of history renders as its own section. The
         section itself is normal top-down flow (its contents are
         ordered oldest-first at render time) — position: sticky
         misbehaves inside column-reverse, so only the scroller
         reverses. */
      .day-section {
        display: flex;
        flex-direction: column;
      }
 
      /* The day marker pins to the top of the chat window while its
         day's messages scroll underneath — position: sticky keeps it
         floating until the section ends, at which point the next
         day's marker takes over. pointer-events pass through so the
         chip never blocks interaction with messages behind it. */
      .day-marker {
        position: sticky;
        top: 0;
        z-index: 3;
        display: flex;
        justify-content: center;
        padding: 0.25em 0 0.75em;
        pointer-events: none;
      }
 
      /* Translucent chip so messages read through it while it floats
         over them mid-scroll. */
      .day-marker .day {
        font-size: 0.7em;
        font-weight: 500;
        letter-spacing: 0.03em;
        color: #71767d;
        background: rgba(255, 255, 255, 0.85);
        backdrop-filter: blur(6px);
        border: 1px solid rgba(0, 0, 0, 0.06);
        border-radius: 999px;
        padding: 3px 12px;
        box-shadow: 0 1px 3px rgba(16, 24, 40, 0.08);
      }
 
      .group-reason {
        text-align: center;
        font-size: 0.75em;
        color: #999;
        margin-bottom: 1em;
        margin-top: 0.5em;
        padding: 0.5em 1em;
        font-style: italic;
      }
 
      .row {
        display: flex;
        flex-direction: row;
        align-items: flex-start;
        margin-bottom: 8px;
      }
 
      .row.is-event {
        margin-bottom: 2px;
        margin-left: 0px !important;
        margin-right: 4px !important;
      }
 
      /* An expanded run of self-contained pill events (field changes,
         group changes, etc. — see CONDENSED_EVENT_TYPES): a collapse
         rail down the left, then the pills flowing as a wrapping,
         centered row. Extra specificity keeps the .incoming .row
         row-reverse rule below from flipping the rail to the right. */
      .group-messages .row.condensed-events {
        flex-direction: row;
      }
 
      /* Expanding animates via the grid 0fr -> 1fr technique so the
         run grows smoothly from the summary's height without any
         measured heights; collapsing plays the same animation in
         reverse (forwards) while the run is in the .collapsing state,
         then the render swaps in the summary pill. */
      .condensed-events .reveal {
        flex-grow: 1;
        min-width: 0;
        display: grid;
        grid-template-rows: 1fr;
        animation: reveal-expand 200ms ease;
        /* floor both animations at the summary pill's height so the
           swap between summary and run happens with no net height
           change — without this the run shrinks to zero and the
           summary pops back up, reading as a false bounce */
        min-height: 24px;
      }
 
      .condensed-events.collapsing .reveal {
        animation: reveal-collapse 180ms ease forwards;
        pointer-events: none;
      }
 
      .condensed-events .reveal-inner {
        overflow: hidden;
        min-width: 0;
      }
 
      .condensed-events .run {
        display: flex;
        flex-direction: row;
        flex-wrap: nowrap;
        align-items: stretch;
        gap: 4px;
      }
 
      @keyframes reveal-expand {
        from {
          grid-template-rows: 0fr;
          opacity: 0;
        }
        to {
          grid-template-rows: 1fr;
          opacity: 1;
        }
      }
 
      @keyframes reveal-collapse {
        from {
          grid-template-rows: 1fr;
          opacity: 1;
        }
        to {
          grid-template-rows: 0fr;
          opacity: 0;
        }
      }
 
      /* the summary pill eases back in after a collapse (and on first
         render — it's subtle enough to double as a gentle entrance) */
      .row.summary-row {
        animation: summary-in 150ms ease;
      }
 
      @keyframes summary-in {
        from {
          opacity: 0;
          transform: translateY(2px);
        }
        to {
          opacity: 1;
          transform: none;
        }
      }
 
      @media (prefers-reduced-motion: reduce) {
        .condensed-events .reveal,
        .condensed-events.collapsing .reveal,
        .row.summary-row {
          animation: none;
        }
      }
 
      .condensed-events .events {
        flex-grow: 1;
        min-width: 0;
        display: flex;
        flex-wrap: wrap;
        justify-content: center;
        align-items: center;
        column-gap: 4px;
        row-gap: 2px;
      }
 
      .condensed-events .event {
        flex-grow: 0;
        max-width: 100%;
      }
 
      /* The rail is the collapse affordance: an icon button at the
         top with a thin line running the height of the run. The whole
         rail is clickable, quietly highlighting on hover. */
      .collapse-rail {
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 3px;
        padding: 1px 2px;
        cursor: pointer;
        --icon-color: #9aa1a9;
      }
 
      .collapse-rail .rail-line {
        flex-grow: 1;
        width: 2px;
        border-radius: 2px;
        background: #e4e7ea;
      }
 
      .collapse-rail:hover {
        --icon-color: #6b7280;
      }
 
      .collapse-rail:hover .rail-line {
        background: #c9cdd2;
      }
 
      .input-panel {
        padding: 1em;
        background: #fff;
      }
 
      temba-user {
        margin-right: 0.6em;
        margin-left: 0.6em;
        width: 2em;
        align-self: flex-end;
      }
 
      .toggle {
        flex-shrink: 0;
        width: 4em;
        height: 4em;
        overflow: hidden;
        border-radius: 100%;
        box-shadow:
          rgba(0, 0, 0, 0.1) 0px 0px 1em 0.7em,
          rgba(0, 0, 0, 0.2) 0px 1px 2px 0px,
          inset 0 0 0 0.25em rgba(0, 0, 0, 0.1);
        cursor: pointer;
        transition: box-shadow var(--toggle-speed, 200ms) ease-out;
        position: absolute;
        bottom: 1em;
        right: 1em;
      }
 
      .toggle:hover {
        box-shadow:
          rgba(0, 0, 0, 0.1) 0px 0px 1em 0.7em,
          rgba(0, 0, 0, 0.4) 0px 1px 2px 0px,
          inset 0 0 0 0.25em rgba(0, 0, 0, 0.2);
      }
 
      .incoming .row {
        flex-direction: row-reverse;
        margin-left: 1em;
      }
 
      /* Bubble fills are soft top-lit gradients derived from the
         same theme vars the flat fills used — color-mix keeps host
         re-theming working — with a whisper of shadow for depth and
         a hairline border mixed just darker than the fill so bubbles
         hold their edge against the background. Base rules carry flat
         fills and pre-mixed borders for browsers without color-mix()
         (pre-Chrome 111); the @supports block below layers the
         gradients on top. */
      .bubble {
        padding: 10px 14px;
        background: var(--color-chat-in, #e5e5ea);
        border-radius: 18px;
        border: var(--chat-border-in, 1px solid #d5d5da);
        box-shadow: 0 1px 2px rgba(16, 24, 40, 0.06);
      }
 
      .bubble .name {
        font-size: 0.95em;
        font-weight: 400;
        color: rgba(0, 0, 0, 0.4);
        margin-bottom: 0.25em;
      }
 
      .outgoing .latest .bubble {
        border-bottom-left-radius: 4px;
      }
 
      .incoming .bubble-wrap {
        align-items: flex-end;
      }
 
      .incoming .bubble {
        background: var(--color-chat-out, #007aff);
        border: var(--chat-border-out, 1px solid #006ee6);
        color: white;
      }
 
      @supports (color: color-mix(in srgb, red, red)) {
        .bubble {
          background: linear-gradient(
            180deg,
            color-mix(in srgb, var(--color-chat-in, #e5e5ea) 62%, white) 0%,
            var(--color-chat-in, #e5e5ea) 100%
          );
          border: var(
            --chat-border-in,
            1px solid
              color-mix(in srgb, var(--color-chat-in, #e5e5ea) 93%, black)
          );
        }
 
        .incoming .bubble {
          background: linear-gradient(
            180deg,
            color-mix(in srgb, var(--color-chat-out, #007aff) 90%, white) 0%,
            var(--color-chat-out, #007aff) 58%,
            color-mix(in srgb, var(--color-chat-out, #007aff) 95%, black) 100%
          );
          border: var(
            --chat-border-out,
            1px solid
              color-mix(in srgb, var(--color-chat-out, #007aff) 90%, black)
          );
        }
      }
 
      .incoming .latest .bubble {
        border-bottom-right-radius: 4px;
      }
 
      .incoming .bubble .name {
        color: rgba(255, 255, 255, 0.7);
      }
 
      /* Notes get the same top-lit gradient treatment as message
         bubbles, derived from their sticky-note yellow. The flat fill
         first is the fallback for browsers without color-mix() — all
         literals here, so the invalid gradient drops at parse time. */
      .note .bubble {
        background: #fffac3;
        background: linear-gradient(
          180deg,
          color-mix(in srgb, #fffac3 62%, white) 0%,
          #fffac3 100%
        );
        border: 1px solid #e8d169;
        color: #5d4e1e;
      }
 
      .note .bubble .name {
        color: rgba(93, 78, 30, 0.65);
      }
 
      .warning .bubble {
        background: #fef3c7;
        color: rgba(125, 87, 18, 0.8);
        border: none;
      }
 
      .warning .bubble .name {
        color: rgba(125, 87, 18, 0.6);
      }
 
      .failed .bubble,
      .error .bubble {
        border: none;
        background: #fee3e3;
        color: #c01829;
      }
 
      .error .bubble .name,
      .failed .bubble .name {
        color: rgba(192, 24, 41, 0.6);
      }
 
      .deleted .bubble,
      .empty .bubble {
        background: #fff;
        color: #999;
        border: 1px solid #e0e0e0;
      }
 
      .deleted .bubble .name,
      .empty .bubble .name {
        color: #aaa;
      }
 
      .typing-dots {
        display: flex;
        align-items: center;
        gap: 4px;
        padding: 4px 0;
      }
 
      .typing-dots span {
        width: 7px;
        height: 7px;
        border-radius: 50%;
        background: currentColor;
        opacity: 0.4;
        animation: typing-bounce 1.2s ease-in-out infinite;
      }
 
      .typing-dots span:nth-child(2) {
        animation-delay: 0.15s;
      }
 
      .typing-dots span:nth-child(3) {
        animation-delay: 0.3s;
      }
 
      @keyframes typing-bounce {
        0%,
        60%,
        100% {
          transform: translateY(0);
          opacity: 0.4;
        }
        30% {
          transform: translateY(-3px);
          opacity: 1;
        }
      }
 
      .message-text {
        white-space: pre-wrap;
        margin-bottom: 0;
        line-height: 1.2;
        word-break: break-word;
        font-size: 13px;
      }
 
      .message-deleted,
      .message-empty {
        font-style: italic;
        margin-bottom: 0;
        line-height: 1.2;
        font-size: 13px;
      }
 
      .quick-replies {
        display: flex;
        flex-wrap: wrap;
        justify-content: center;
        gap: 6px;
        padding: 8px 0;
        margin: 0 auto;
        max-width: 90%;
      }
 
      .quick-reply-btn {
        padding: 4px 8px;
        border-radius: 18px;
        border: 1px solid var(--color-chat-out, #007aff);
        background: white;
        color: var(--color-chat-out, #007aff);
        font-size: 11px;
        cursor: pointer;
        transition: all 0.2s ease;
      }
 
      .quick-reply-btn:hover:not(:disabled) {
        background: var(--color-chat-out, #007aff);
        color: white;
      }
 
      .quick-reply-btn:disabled {
        opacity: 0.5;
        cursor: not-allowed;
      }
 
      .chat {
        width: 28rem;
        border-radius: var(--curvature);
        overflow: hidden;
        box-shadow:
          rgba(0, 0, 0, 0.1) 0px 3px 7px 0px,
          rgba(0, 0, 0, 0.2) 0px 1px 2px 0px,
          rgba(0, 0, 0, 0.1) 5em 5em 5em 5em;
        position: absolute;
        bottom: 3em;
        right: 1em;
        transition: all var(--toggle-speed, 200ms) ease-out;
        transform: scale(0.9);
        pointer-events: none;
        opacity: 0;
      }
 
      .chat.open {
        bottom: 6em;
        opacity: 1;
        transform: scale(1);
        pointer-events: initial;
      }
 
      .messages {
        position: relative;
        flex-grow: 1;
        overflow: hidden;
      }
 
      .scroll {
        position: absolute;
        top: 0;
        bottom: 0;
        right: 0;
        left: 0;
        overflow-y: auto;
        overflow-x: hidden;
        -webkit-overflow-scrolling: touch;
        overflow-scrolling: touch;
        padding: var(--chat-top-padding, 1em) 1em
          var(--chat-bottom-padding, 2.5em) 1em;
        display: flex;
        flex-direction: column-reverse;
      }
 
      :host(.phone-screen) .scroll {
        scrollbar-width: none;
        -ms-overflow-style: none;
      }
 
      :host(.phone-screen) .scroll::-webkit-scrollbar {
        display: none;
      }
 
      /* Top/bottom scroll-shadow indicators. Decorative only — they
         must not intercept clicks (would otherwise block the chat
         scrollbar and the bottom edge of the messages area). */
      .messages:before {
        content: '';
        background: radial-gradient(
            farthest-side at 50% 0,
            rgba(0, 0, 0, 0.2),
            rgba(0, 0, 0, 0)
          )
          center top;
        height: 10px;
        display: block;
        position: absolute;
        width: 100%;
        transition: opacity var(--toggle-speed, 200ms) ease-out;
        z-index: 1;
        pointer-events: none;
      }
 
      .messages:after {
        content: '';
        background: radial-gradient(
            farthest-side at 50% 100%,
            rgba(0, 0, 0, 0.2),
            rgba(0, 0, 0, 0)
          )
          center bottom;
        height: 10px;
        display: block;
        position: absolute;
        bottom: 0;
        margin-top: -10px;
        width: 100%;
        margin-right: 5em;
        transition: opacity var(--toggle-speed, 200ms) ease-out;
        z-index: 1;
        pointer-events: none;
      }
 
      .bubble-wrap {
        position: relative;
        max-width: 70%;
        display: flex;
        flex-direction: column;
        align-items: flex-start;
      }
 
      .scroll-at-top.messages:before {
        opacity: 0;
      }
 
      .scroll-at-bottom.messages:after {
        opacity: 0;
      }
 
      .input {
        border: none;
        flex-grow: 1;
        color: #333;
        font-size: 1em;
      }
 
      .input:focus {
        outline: none;
      }
 
      input::placeholder {
        opacity: 0.3;
      }
 
      .input.inactive {
        // pointer-events: none;
        // opacity: 0.3;
      }
 
      .active {
      }
 
      .send-icon {
        color: #eee;
        pointer-events: none;
        transform: rotate(-45deg);
        transition: transform 0.2s ease-out;
      }
 
      .pending .send-icon {
        color: var(--color-primary-dark);
        pointer-events: initial;
        transform: rotate(0deg);
      }
 
      .notice {
        padding: 1em;
        background: #f8f8f8;
        color: #666;
        text-align: center;
        cursor: pointer;
      }
 
      .connecting .notice {
        display: flex;
        justify-content: center;
      }
 
      .connecting .notice temba-icon {
        margin-left: 0.5em;
      }
 
      .reconnect {
        color: var(--color-primary-dark);
        text-decoration: underline;
        font-size: 0.9em;
      }
 
      .input:disabled {
        background: transparent !important;
      }
 
      temba-loading {
        justify-content: center;
        margin: 0.5em auto;
        margin-bottom: 2em;
      }
 
      temba-loading.hidden {
        display: none;
      }
 
      .inline {
      }
 
      .event {
        flex-grow: 1;
        align-self: center;
        display: flex;
        flex-direction: column;
        align-items: center;
        text-align: center;
        font-size: 11px;
        color: #8e8e93;
        max-width: 100%;
        overflow: hidden;
      }
 
      .event .webhook-event {
        display: inline-flex;
        align-items: flex-start;
        gap: 6px;
        max-width: 100%;
        min-width: 0;
      }
 
      .event .webhook-event-text {
        overflow: hidden;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        word-break: break-all;
        min-width: 0;
        padding: 4px 0;
      }
 
      .event .webhook-event-url {
        color: inherit;
        text-decoration: underline;
        cursor: pointer;
      }
 
      .event .webhook-event-url:hover {
        text-decoration: none;
      }
 
      .event p {
        margin: 0;
        padding: 0;
      }
 
      .event strong {
        font-weight: 500;
      }
 
      .collapse {
      }
 
      a {
        color: var(--color-primary-dark);
      }
 
      .attachments {
        display: flex;
        flex-direction: row;
        flex-wrap: wrap;
        align-items: center;
        align-self: flex-start;
      }
 
      .incoming .attachments {
        align-self: flex-end;
      }
 
      temba-thumbnail {
        margin: 0.4em;
        border-radius: var(--curvature);
      }
 
      .failed temba-thumbnail,
      .error temba-thumbnail {
        --thumb-background: #ffe6e6;
        --thumb-border: var(--color-error);
        border: 1px solid var(--color-error);
        color: #ad4747a8;
      }
 
      .outgoing .popup {
        justify-content: left;
      }
 
      .incoming .popup {
        justify-content: right;
      }
 
      .popup {
        display: flex;
        align-items: center;
        position: absolute;
        background: #fff;
        margin: 0;
        /* same treatment as the metadata line in the event tooltips */
        font-size: 11px;
        padding: 6px 10px;
        border-radius: var(--curvature);
        box-shadow:
          rgba(0, 0, 0, 0.05) 0px 3px 7px 0px,
          rgba(0, 0, 0, 0.2) 0px 1px 2px 0px;
        border: 1px solid #f3f3f3;
        opacity: 0;
        visibility: hidden;
        /* sit a small gap above the bubble regardless of popup height
           so the arrow bridges it, matching the event tooltips — the
           hoverable linger makes the gap crossable */
        bottom: calc(100% + 6px);
        transform: translateY(6px);
        z-index: 2;
        /* linger briefly on unhover so the mouse can travel into the
           popup to select or click its contents, then slide back down
           while fading; visibility flips once the fade finishes and
           keeps the hidden popup from swallowing mouse events over
           the bubble */
        transition:
          opacity 120ms cubic-bezier(0.2, 0, 0, 1) 300ms,
          transform 120ms cubic-bezier(0.2, 0, 0, 1) 300ms,
          visibility 0s linear 420ms;
      }
 
      .popup .log-link {
        margin-left: 1em;
        color: #999;
      }
 
      .popup .log-link:hover {
        color: var(--color-primary-dark);
      }
 
      .popup .arrow {
        z-index: 1;
        position: absolute;
        bottom: -9px;
        width: 16px;
        height: 11px;
        /* the shadow is on this wrapper rather than the clipped
           shape below so it hugs the triangle's slanted edges */
        filter: drop-shadow(0 2px 2px rgba(0, 0, 0, 0.12));
      }
 
      /* a CSS-drawn tail rather than a ▼ glyph — glyph ink lands
         wherever the platform font puts it, so the tip rendered
         hidden on some systems and overshot on others. This triangle
         is deterministic: its top 2px lie on the popup body hiding
         the border where the tail meets it, the rest bridges the gap
         below so the tip visibly laps over the top of the bubble */
      .popup .arrow::after {
        content: '';
        display: block;
        width: 100%;
        height: 100%;
        background: #fff;
        clip-path: polygon(0 0, 100% 0, 50% 100%);
      }
 
      .bubble-wrap:hover .popup {
        opacity: 1;
        visibility: visible;
        transform: translateY(0);
        /* same 350ms hover-intent delay as the event pill tips, then
           slide up into position */
        transition:
          opacity 120ms cubic-bezier(0.2, 0, 0, 1) 350ms,
          transform 120ms cubic-bezier(0.2, 0, 0, 1) 350ms,
          visibility 0s linear 350ms;
      }
 
      .new-message-notification {
        position: absolute;
        bottom: 1em;
        left: 50%;
        transform: translateX(-50%) translateY(100px);
        background: var(--color-primary-dark, #3c92dd);
        color: white;
        padding: 0.75em 1.5em;
        border-radius: var(--curvature);
        box-shadow:
          rgba(0, 0, 0, 0.2) 0px 3px 7px 0px,
          rgba(0, 0, 0, 0.3) 0px 1px 2px 0px;
        cursor: pointer;
        opacity: 0;
        transition: all 0.3s ease-out;
        z-index: 100;
        font-weight: 500;
        pointer-events: none;
      }
 
      .new-message-notification.visible {
        transform: translateX(-50%) translateY(0);
        opacity: 1;
        pointer-events: auto;
      }
 
      .new-message-notification:hover {
        background: var(--color-primary-darker, #2b7ac4);
        box-shadow:
          rgba(0, 0, 0, 0.3) 0px 4px 10px 0px,
          rgba(0, 0, 0, 0.4) 0px 2px 4px 0px;
      }
 
      mark {
        background: #fef08a;
        color: #1a1a1a;
        padding: 0.1em 0.3em;
        margin: 0 0.1em;
        border-radius: 3px;
        box-shadow: 0 1px 2px rgba(0, 0, 0, 0.15);
      }
 
      .search-match .bubble {
        box-shadow: 0 0 0 2px #fef08a;
      }
 
      @keyframes search-pulse {
        0% {
          box-shadow: 0 0 0 2px #fef08a;
        }
        50% {
          box-shadow: 0 0 0 4px #fef08a;
        }
        100% {
          box-shadow: 0 0 0 2px #fef08a;
        }
      }
 
      .search-match .bubble {
        animation: search-pulse 1s ease-in-out 2;
      }
    `;
  }
 
  @property({ type: Array })
  messageGroups: MessageGroup[] = [];
 
  @property({ type: Boolean })
  fetching = false;
 
  @property({ type: Boolean, attribute: false })
  hideTopScroll = true;
 
  @property({ type: Boolean, attribute: false })
  hideBottomScroll = true;
 
  @property({ type: String, attribute: 'avatar' })
  defaultAvatar = DEFAULT_AVATAR;
 
  @property({ type: Boolean })
  agent = false;
 
  @property({ type: Boolean })
  avatars = false;
 
  // identity of the contact this chat belongs to, used to render a
  // name-based avatar for the contact's own incoming messages (which the
  // backend does not attach a `_user` to)
  @property({ type: String })
  contactName: string;
 
  @property({ type: String })
  contactUuid: string;
 
  @property({ type: Boolean, attribute: false })
  endOfHistory = false;
 
  @property({ type: Object, attribute: false })
  oldestEventDate: Date = null;
 
  @property({ type: Boolean, attribute: false })
  showNewMessageNotification = false;
 
  @property({ type: Object })
  showMessageLogsAfter: Date = null;
 
  @property({ type: Boolean })
  hasFooter = false;
 
  @property({ type: Boolean })
  showTimestamps = true;
 
  @property({ type: String, attribute: false })
  searchHighlight: string = null;
 
  @property({ type: String, attribute: false })
  highlightMessageUuid: string = null;
 
  private msgMap = new Map<string, ContactEvent>();
 
  // runs of informational events collapse behind a summary pill —
  // this tracks which runs the user has expanded as a set of event
  // ids (a run is expanded if any of its events is present). keying
  // on membership rather than the run's first id keeps expansion
  // stable when pagination prepends older events into the run
  private expandedEventChunks = new Set<string>();
 
  // runs mid-collapse: they keep rendering expanded (with the closing
  // animation) until the animation finishes, then swap to the summary
  private collapsingEventChunks = new Set<string>();
  private metadataCache = new Map<string, ContactEvent>();
 
  // ephemeral typing indicators by author key - they live in msgMap and
  // messageGroups like real messages (so they group and render normally)
  // but are added and removed through setTyping / clearTyping
  private typingEvents = new Map<string, TypingEvent>();
  private typingTimeouts = new Map<string, number>();
 
  // bumped on reset so deferred addMessages work from before a reset is
  // discarded rather than re-merged into the fresh view
  private resetGeneration = 0;
 
  public disconnectedCallback(): void {
    super.disconnectedCallback();
    // clear pending typing decay timers so no callback fires on a detached
    // element (typingEvents/msgMap are left intact for a possible reconnect)
    this.typingTimeouts.forEach((timeout) => window.clearTimeout(timeout));
    this.typingTimeouts.clear();
  }
 
  public firstUpdated(
    changed: PropertyValueMap<any> | Map<PropertyKey, unknown>
  ): void {
    super.firstUpdated(changed);
    const scroll = this.shadowRoot.querySelector('.scroll');
    const hasScroll = scroll.scrollHeight > scroll.clientHeight;
    this.hideBottomScroll = true;
    this.hideTopScroll = !hasScroll;
  }
 
  public addMessages(
    messages: ContactEvent[],
    startTime: Date = null,
    append = false,
    maintainScroll = false
  ) {
    if (!startTime) {
      startTime = new Date();
    }
 
    const generation = this.resetGeneration;
    const elapsed = new Date().getTime() - startTime.getTime();
    window.setTimeout(
      () => {
        this.fetching = false;
 
        // the chat was reset while we were waiting, our messages are stale
        if (generation !== this.resetGeneration) {
          return;
        }
        // first add messages to the map
        const newMessages = [];
        for (const m of messages) {
          // filter out metadata events - they aren't rendered but cached for later reference
          if (m.type === 'msg_deleted' || m.type === 'msg_status_changed') {
            const msgUuid = (m as any).msg_uuid;
            if (msgUuid) {
              this.metadataCache.set(msgUuid, m);
            }
            continue;
          }
 
          if (this.addMessage(m)) {
            newMessages.push(m.uuid);
          }
        }
 
        if (newMessages.length === 0) {
          // nothing rendered (a metadata-only or duplicate page) —
          // still settle the fetch and keep the chain going, since an
          // unscrollable view has no other way to ask for the rest of
          // history
          this.fireCustomEvent(CustomEventType.FetchComplete);
          this.requestMoreIfUnscrollable();
          return;
        }
 
        const ele = this.shadowRoot.querySelector('.scroll');
        const prevTop = ele.scrollTop;
        const prevScrollHeight = ele.scrollHeight;
        const scrollableHeight = ele.scrollHeight - ele.clientHeight;
        const isScrolledAway =
          scrollableHeight > 0 && Math.abs(ele.scrollTop) > 50;
 
        // a real message from someone typing replaces their indicator, and
        // any remaining indicators re-float below the appended messages
        if (append) {
          this.clearTypingForAuthors(
            newMessages.map((uuid) => this.msgMap.get(uuid))
          );
        }
 
        const grouped = this.groupMessages(newMessages);
        this.insertGroups(grouped, append);
 
        if (append) {
          this.refloatTyping();
        }
 
        // show notification if new messages are appended and user is scrolled away from bottom
        // but not during search (searchHighlight is set)
        if (
          append &&
          isScrolledAway &&
          newMessages.length > 0 &&
          !this.searchHighlight
        ) {
          this.showNewMessageNotification = true;
        }
 
        window.setTimeout(() => {
          if (generation !== this.resetGeneration) {
            return;
          }
 
          // when appending (new messages at bottom), adjust scroll to maintain visible content
          // with column-reverse, new content at bottom increases scrollHeight
          if (append && (isScrolledAway || maintainScroll)) {
            const heightDiff = ele.scrollHeight - prevScrollHeight;
            ele.scrollTop = prevTop - heightDiff;
          } else {
            ele.scrollTop = prevTop;
          }
 
          // recalculate shadow visibility after content change
          const scrollableHeight = ele.scrollHeight - ele.clientHeight;
          const absScrollTop = Math.abs(ele.scrollTop);
          this.hideTopScroll = absScrollTop >= scrollableHeight - 1;
          this.hideBottomScroll = absScrollTop <= 1;
 
          this.fireCustomEvent(CustomEventType.FetchComplete);
          this.requestMoreIfUnscrollable();
        }, 100);
      },
      // if it's the first load don't wait, otherwise wait a minimum amount of time
      this.messageGroups.length === 0
        ? 0
        : Math.max(0, MIN_FETCH_TIME - elapsed)
    );
  }
 
  /**
   * Synchronously loads messages into the chat without any timeouts or scroll
   * adjustments. Use this when the caller controls visibility and scroll
   * positioning (e.g. search result navigation).
   */
  public loadMessages(messages: ContactEvent[]) {
    const newMessageIds: string[] = [];
    for (const m of messages) {
      if (m.type === 'msg_deleted' || m.type === 'msg_status_changed') {
        const msgUuid = (m as any).msg_uuid;
        if (msgUuid) {
          this.metadataCache.set(msgUuid, m);
        }
        continue;
      }
      if (this.addMessage(m)) {
        newMessageIds.push(m.uuid);
      }
    }
    if (newMessageIds.length > 0) {
      const grouped = this.groupMessages(newMessageIds);
      this.insertGroups(grouped, false);
    }
  }
 
  private addMessage(msg: ContactEvent): boolean {
    const isNew = !this.messageExists(msg);
    this.msgMap.set(msg.uuid, msg);
    return isNew;
  }
 
  /**
   * Shows a typing indicator for the event's author, appended to the
   * newest messages so it groups with them. Repeat pulses from the same
   * author just refresh the decay timer; without fresh pulses or an
   * explicit clearTyping the indicator decays on its own.
   */
  public setTyping(event: TypingEvent) {
    const key = getTypingKey(event);
    const shown = this.typingEvents.get(key);
    if (shown) {
      // keep the indicator's timestamp fresh so a long-lived one doesn't
      // drift across the grouping time window and split into its own group
      shown.created_on = event.created_on;
    } else {
      this.typingEvents.set(key, event);
      this.addMessage(event);
      this.insertGroups(this.groupMessages([event.uuid]), true);
    }
 
    const existing = this.typingTimeouts.get(key);
    if (existing) {
      window.clearTimeout(existing);
    }
    this.typingTimeouts.set(
      key,
      window.setTimeout(() => this.removeTyping(key), TYPING_TIMEOUT)
    );
  }

  /**
   * Clears the typing indicator for the event's author, or all indicators
   * when no event is given.
   */
  public clearTyping(event?: TypingEvent) {
    if (event) {
      this.removeTyping(getTypingKey(event));
    } else {
      for (const key of [...this.typingEvents.keys()]) {
        this.removeTyping(key);
      }
    }
  }
 
  private removeTyping(key: string) {
    const timeout = this.typingTimeouts.get(key);
    if (timeout) {
      window.clearTimeout(timeout);
      this.typingTimeouts.delete(key);
    }
 
    const event = this.typingEvents.get(key);
    if (event) {
      this.typingEvents.delete(key);
      this.removeEvent(event.uuid);
    }
  }
 
  private removeEvent(uuid: string) {
    this.msgMap.delete(uuid);
    for (let i = 0; i < this.messageGroups.length; i++) {
      const group = this.messageGroups[i];
      const idx = group.messages.indexOf(uuid);
      if (idx >= 0) {
        group.messages.splice(idx, 1);
        if (group.messages.length === 0) {
          this.messageGroups.splice(i, 1);
        }
        break;
      }
    }
    this.requestUpdate('messageGroups');
  }
 
  // an author's real message replaces their typing indicator
  private clearTypingForAuthors(events: ContactEvent[]) {
    for (const event of events) {
      if (event.type === 'typing_started') {
        continue;
      }
      const key = event._user?.uuid
        ? event._user.uuid
        : event.type === 'msg_received'
          ? 'contact'
          : null;
      if (key && this.typingEvents.has(key)) {
        this.removeTyping(key);
      }
    }
  }

  // re-appends active typing indicators so they always sit below the
  // newest messages
  private refloatTyping() {
    if (this.typingEvents.size === 0) {
      return;
    }
    const events = [...this.typingEvents.values()];
    for (const event of events) {
      this.removeEvent(event.uuid);
    }
    for (const event of events) {
      this.addMessage(event);
    }
    this.insertGroups(
      this.groupMessages(events.map((event) => event.uuid)),
      true
    );
  }
 
  public messageExists(msg: ContactEvent): boolean {
    return this.msgMap.has(msg.uuid);
  }
 
  private isSameGroup(
    msg1: ContactEvent,
    msg2: ContactEvent
  ): { same: boolean; reason?: GroupReason } {
    if (!msg1 || !msg2) {
      return { same: true };
    }
 
    // for type equivalence, treat all non-message types as the same.
    // Notes are treated like messages so they group with other notes
    // only when type + author match (and split from non-note events).
    const type1 = effectiveType(msg1);
    const type2 = effectiveType(msg2);
    const isMsg1 =
      type1 === 'msg_created' ||
      type1 === 'msg_received' ||
      type1 === 'ivr_created' ||
      type1 === 'ticket_note_added';
    const isMsg2 =
      type2 === 'msg_created' ||
      type2 === 'msg_received' ||
      type2 === 'ivr_created' ||
      type2 === 'ticket_note_added';
    const typeMatch = isMsg1 && isMsg2 ? type1 === type2 : isMsg1 === isMsg2;
 
    // the only time boundary is the calendar day (the chat renders
    // day sections with sticky markers, so groups must align to
    // days) — within a day, hours apart doesn't split a group, so
    // e.g. a run expiry and a campaign start hours later still
    // condense with the events between them
    if (getDayKey(msg1.created_on) !== getDayKey(msg2.created_on)) {
      return { same: false, reason: 'time_elapsed' };
    }
 
    if (!typeMatch) {
      return { same: false, reason: 'new_type' };
    }
 
    // only check author for message types
    if (isMsg1 && isMsg2 && msg1._user?.name !== msg2._user?.name) {
      return { same: false, reason: 'new_author' };
    }
 
    return { same: true };
  }
 
  private insertGroups(newGroups: MessageGroup[], append = false) {
    if (!append) {
      newGroups.reverse();
    }
 
    for (const newGroup of newGroups) {
      // see if our new group belongs to the most recent group
      const group =
        this.messageGroups[append ? 0 : this.messageGroups.length - 1];
 
      if (group) {
        const lastMsgId = group.messages[group.messages.length - 1];
        const lastMsg = this.msgMap.get(lastMsgId);
        const newMsg = this.msgMap.get(newGroup.messages[0]);
        // if our message belongs to the previous group, in we go
        const groupCheck = this.isSameGroup(lastMsg, newMsg);
        if (groupCheck.same) {
          if (append) {
            group.messages.push(...newGroup.messages);
          } else {
            // historical messages are older, prepend to maintain chronological order
            group.messages.unshift(...newGroup.messages);
          }
        } else {
          // otherwise, just add our entire group as a new one
          if (append) {
            this.messageGroups.splice(0, 0, newGroup);
          } else {
            // update the boundary group's reason to reflect the new adjacency
            if (groupCheck.reason) {
              group.reason = groupCheck.reason;
            }
            this.messageGroups.push(newGroup);
          }
        }
      } else {
        if (append) {
          this.messageGroups.splice(0, 0, newGroup);
        } else {
          this.messageGroups.push(newGroup);
        }
      }
    }
 
    this.requestUpdate('messageGroups');
  }
 
  private groupMessages(msgIds: string[]): MessageGroup[] {
    // group our messages by origin and user
    const groups: MessageGroup[] = [];
    let lastGroup: MessageGroup = null;
    let lastMsg: ContactEvent = null;
 
    for (const msgId of msgIds) {
      const msg = this.msgMap.get(msgId);
      const groupCheck = this.isSameGroup(msg, lastMsg);
 
      if (!groupCheck.same || !lastGroup) {
        lastGroup = {
          messages: [],
          reason: groupCheck.reason || 'initial'
        };
        groups.push(lastGroup);
      }
 
      lastGroup.messages.push(msgId);
      lastMsg = msg;
    }
    return groups;
  }

  /**
   * Older history is fetched from scroll position, but a fetched page
   * can render shorter than the viewport (condensed event runs
   * collapse a whole page into a line or two), leaving nothing to
   * scroll and no way to request more. After content settles, keep
   * asking for history until the view can meaningfully scroll — the
   * host's own guards (in-flight fetch, end of history) stop the
   * chain.
   */
  private requestMoreIfUnscrollable() {
    const ele = this.shadowRoot.querySelector('.scroll');
    if (ele && ele.scrollHeight - ele.clientHeight <= SCROLL_FETCH_BUFFER) {
      this.fireCustomEvent(CustomEventType.ScrollThreshold);
    }
  }
 
  private handleScroll(event: any) {
    const ele = event.target;
    const scrollableHeight = ele.scrollHeight - ele.clientHeight;
 
    if (scrollableHeight <= 0) {
      return;
    }
 
    // with column-reverse, scrollTop behavior depends on the browser
    // check if scrollTop is negative (some browsers) or positive (others)
    const absScrollTop = Math.abs(ele.scrollTop);
 
    // when scrolling up to older messages, absScrollTop increases
    // trigger when we're close to the maximum scroll (oldest messages)
    const shouldFetch = absScrollTop >= scrollableHeight - SCROLL_FETCH_BUFFER;
 
    this.hideTopScroll = absScrollTop >= scrollableHeight - 1;
    this.hideBottomScroll = absScrollTop <= 1;
 
    // hide notification when scrolled to bottom
    if (absScrollTop <= 10) {
      this.showNewMessageNotification = false;
    }
 
    if (shouldFetch) {
      this.fireCustomEvent(CustomEventType.ScrollThreshold);
    }
 
    // trigger when scrolling near the bottom (newest messages) in column-reverse
    if (absScrollTop <= SCROLL_FETCH_BUFFER * 3 && scrollableHeight > 0) {
      this.fireCustomEvent(CustomEventType.ScrollThresholdBottom);
    }
  }
 
  public scrollToBottom() {
    const scroll = this.shadowRoot.querySelector('.scroll');
    if (scroll) {
      scroll.scrollTop = 0;
      this.hideBottomScroll = true;
      this.showNewMessageNotification = false;
    }
  }
 
  public scrollToMessage(
    uuid: string,
    animate = true,
    onComplete?: () => void
  ) {
    this.highlightMessageUuid = uuid;
    window.setTimeout(() => {
      const scroll = this.shadowRoot.querySelector('.scroll') as HTMLElement;
      const row = this.shadowRoot.querySelector(
        `.row[data-uuid="${uuid}"]`
      ) as HTMLElement;
      if (scroll && row) {
        // manually set scrollTop to center the row within the scroll area
        // this avoids scrollIntoView which can move ancestor containers
        const scrollRect = scroll.getBoundingClientRect();
        const rowRect = row.getBoundingClientRect();
        const rowCenter = rowRect.top + rowRect.height / 2;
        const scrollCenter = scrollRect.top + scrollRect.height / 2;
        const offset = rowCenter - scrollCenter;
        if (animate) {
          scroll.scrollBy({ top: offset, behavior: 'smooth' });
        } else {
          scroll.scrollTop = scroll.scrollTop + offset;
        }
      }
      if (onComplete) {
        // wait one frame for the browser to finish the scroll
        requestAnimationFrame(() => onComplete());
      }
    }, 150);
  }
 
  private highlightText(text: string, search: string): TemplateResult | string {
    if (!search || !text) {
      return text;
    }
    const escaped = search.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
    const regex = new RegExp(`(${escaped})`, 'gi');
    const parts = text.split(regex);
    if (parts.length === 1) {
      return text;
    }
    return html`${parts.map((part, index) =>
      index % 2 === 1 ? html`<mark>${part}</mark>` : part
    )}`;
  }
 
  private handleNewMessageClick() {
    this.scrollToBottom();
  }
 
  private getReasonLabel(reason: GroupReason): string {
    switch (reason) {
      case 'new_author':
        return '👤 Different author';
      case 'new_type':
        return '🔄 Message type changed';
      case 'time_elapsed':
      case 'initial':
      default:
        return '';
    }
  }

  private renderMessageGroup(group: MessageGroup): TemplateResult {
    const msgIds = group.messages;

    const mostRecentId = msgIds[msgIds.length - 1];
    const currentMsg = this.msgMap.get(mostRecentId);

    const currentType = effectiveType(currentMsg);
    const incoming = this.agent
      ? currentType !== 'msg_received'
      : currentType === 'msg_received';

    const name = currentMsg._user?.name;

    const isMessageType =
      currentType === 'msg_received' ||
      currentType === 'msg_created' ||
      currentType === 'ivr_created' ||
      currentType === 'ticket_note_added';
    const showAvatar =
      this.avatars && ((isMessageType && this.agent) || !incoming);

    // resolve the identity shown in the avatar: prefer the user attached to
    // the event (an agent or flow author), otherwise fall back to the contact
    // for their own incoming messages.
    //
    // contact fallback assumes `_user` is absent for `msg_received` (contact
    // messages carry no `_user`, so first_name/last_name aren't available and
    // getFullName falls back to `name`); the fallback only applies when there
    // is no `_user` on the event.
    const fromContact = currentType === 'msg_received' && !currentMsg._user;
    const avatarName = currentMsg._user
      ? currentMsg._user.name
      : fromContact
        ? this.contactName
        : undefined;
    const avatarUuid = currentMsg._user
      ? currentMsg._user.uuid
      : fromContact
        ? this.contactUuid
        : undefined;
 
    // determine whether to fall back to the generic default (system) avatar.
    // when the event has a `_user`, preserve the original behavior exactly:
    // system iff that user has no uuid (a name-only flow author still gets the
    // default avatar). for a contact event with no `_user`, it's system only
    // when we have no contact identity at all.
    const isSystem = currentMsg._user
      ? !currentMsg._user.uuid
      : fromContact
        ? !this.contactUuid && !this.contactName
        : true;

    const reasonLabel = this.getReasonLabel(group.reason);
    const showReason = false; // reasonLabel && idx > 0;
 
    // chunk the group into runs — consecutive condensable pill events
    // share a single wrapping row, everything else renders one per row
    const chunks: { condensed: boolean; ids: string[]; start: number }[] = [];
    msgIds.forEach((msgId, index) => {
      const msg = this.msgMap.get(msgId);
      const condensed =
        msg?._rendered?.type === MessageType.Inline &&
        CONDENSED_EVENT_TYPES.has(msg.type);
      const last = chunks[chunks.length - 1];
      if (last && last.condensed && condensed) {
        last.ids.push(msgId);
      } else {
        chunks.push({ condensed, ids: [msgId], start: index });
      }
    });
 
    const resultHtml = html`
      <div class="block  ${incoming ? 'incoming' : 'outgoing'}">
        <div class="group-messages" style="flex-grow:1">
          ${repeat(
            chunks,
            (chunk) => chunk.ids[0],
            (chunk) => {
              if (chunk.condensed) {
                // condensable events collapse behind a single summary
                // pill until expanded — even a run of one, so single
                // events behave the same as longer runs
                const expanded = chunk.ids.some((id) =>
                  this.expandedEventChunks.has(id)
                );
                if (!expanded) {
                  const events = chunk.ids.map((id) => this.msgMap.get(id));
                  return html`<div class="row message is-event summary-row">
                    <div class="event">
                      ${renderEventSummary(events, () => {
                        chunk.ids.forEach((id) =>
                          this.expandedEventChunks.add(id)
                        );
                        this.requestUpdate();
                      })}
                    </div>
                  </div>`;
                }
 
                // collapsing runs keep rendering expanded while the
                // closing animation plays, then swap to the summary
                const collapsing = chunk.ids.some((id) =>
                  this.collapsingEventChunks.has(id)
                );
                return html`<div
                  class="row message is-event condensed-events ${collapsing
                    ? 'collapsing'
                    : ''}"
                >
                  <div class="reveal">
                    <div class="reveal-inner">
                      <div class="run">
                        <div
                          class="collapse-rail"
                          title="Collapse"
                          @click=${() => {
                            if (collapsing) {
                              return;
                            }
                            chunk.ids.forEach((id) =>
                              this.collapsingEventChunks.add(id)
                            );
                            this.requestUpdate();
                            window.setTimeout(() => {
                              chunk.ids.forEach((id) => {
                                this.collapsingEventChunks.delete(id);
                                this.expandedEventChunks.delete(id);
                              });
                              this.requestUpdate();
                            }, 180);
                          }}
                        >
                          <temba-icon name="up" size="0.85"></temba-icon>
                          <div class="rail-line"></div>
                        </div>
                        <div class="events">
                          ${chunk.ids.map((msgId) => {
                            const msg = this.msgMap.get(msgId);
                            return html`<div
                              class="event"
                              data-uuid=${msg.uuid || nothing}
                            >
                              ${msg._rendered.html}
                            </div>`;
                          })}
                        </div>
                      </div>
                    </div>
                  </div>
                </div>`;
              }
              return chunk.ids.map((msgId, chunkIndex) => {
                const index = chunk.start + chunkIndex;
                const msg = this.msgMap.get(msgId);
                const msgEvent = msg as MsgEvent;
                const statusClass = (msg as any)._status
                  ? (msg as any)._status.status
                  : '';
                const hasError =
                  msgEvent.msg?.unsendable_reason ||
                  (msgEvent._status?.reason &&
                    (statusClass === 'failed' || statusClass === 'errored'));
                const unsendableClass = hasError ? 'error' : '';
                const deletedClass = msgEvent._deleted ? 'deleted' : '';
                const emptyClass = isEmptyMsg(msgEvent) ? 'empty' : '';
                const latestClass = index === msgIds.length - 1 ? 'latest' : '';
                const eventClass = msg._rendered ? 'is-event' : '';
                const noteClass =
                  msg.type === 'ticket_note_added' ? 'note' : '';
                const typingClass =
                  msg.type === 'typing_started' ? 'typing' : '';
                const matchClass =
                  this.highlightMessageUuid === msg.uuid ? 'search-match' : '';
                return html`<div
                  class="row message ${statusClass} ${unsendableClass} ${deletedClass} ${emptyClass} ${latestClass} ${eventClass} ${noteClass} ${typingClass} ${matchClass}"
                  data-uuid=${msg.uuid || nothing}
                >
                  ${this.renderMessage(msg, index == 0 ? name : null)}
                </div>`;
              });
            }
          )}
        </div>
        ${showAvatar
          ? html`<div class="avatar" style="align-self:flex-end">
              <temba-user
                uuid=${avatarUuid ?? nothing}
                name=${avatarName ?? nothing}
                first_name=${currentMsg._user?.first_name ?? nothing}
                last_name=${currentMsg._user?.last_name ?? nothing}
                avatar=${currentMsg._user?.avatar ?? nothing}
                ?system=${isSystem}
              >
              </temba-user>
            </div>`
          : null}
      </div>
      ${showReason
        ? html`<div class="group-reason">${reasonLabel}</div>`
        : null}
    `;
 
    return resultHtml;
  }
 
  private renderNote(event: TicketEvent, name: string | null): TemplateResult {
    // notes carry their time the same way messages do — in the
    // hover popup — rather than printing it under the bubble
    return html`<div class="bubble-wrap">
      ${this.showTimestamps
        ? html`<div class="popup" style="white-space: nowrap;">
            <temba-date
              value="${event.created_on.toISOString()}"
              display="datetime"
            ></temba-date>
            <div class="arrow"></div>
          </div>`
        : null}
      <div class="bubble">
        ${name ? html`<div class="name">${name}</div>` : null}
        <div style="white-space: pre-wrap;">${event.note}</div>
      </div>
    </div>`;
  }
 
  private renderMessage(event: ContactEvent, name = null): TemplateResult {
    if (event._rendered) {
      return html`<div class="event">${event._rendered.html}</div>`;
    }
 
    if (event.type === 'ticket_note_added') {
      return this.renderNote(event as TicketEvent, name);
    }
 
    if (event.type === 'typing_started') {
      return html`<div class="bubble-wrap">
        <div class="bubble">
          ${name ? html`<div class="name">${name}</div>` : null}
          <div class="typing-dots"><span></span><span></span><span></span></div>
        </div>
      </div>`;
    }
 
    const message = event as MsgEvent;
 
    // safety check: if msg doesn't exist, return nothing
    if (!message.msg) {
      return html``;
    }
 
    const unsendableReason = message.msg?.unsendable_reason;
    const statusReason = message._status?.reason;
    const errorMessage = unsendableReason
      ? getUnsendableReasonMessage(unsendableReason)
      : statusReason
        ? getStatusReasonMessage(statusReason)
        : null;
 
    const logsURL =
      this.showMessageLogsAfter &&
      message.created_on >= this.showMessageLogsAfter &&
      message.msg.channel
        ? `/channels/channel/logs/${message.msg.channel.uuid}/msg/${event.uuid}/`
        : null;
 
    // handle deleted messages
    const isDeleted = message._deleted;
    const deletedByText = isDeleted
      ? message._deleted.by_contact
        ? 'contact'
        : message._deleted.user?.name || 'user'
      : null;
 
    // messages with no text and no attachments get a muted placeholder
    const isEmpty = isEmptyMsg(message);
 
    // check if message has location attachment and text is just coordinates
    const hasLocationAttachment = message.msg.attachments?.some((att) =>
      attachmentAsString(att).startsWith('geo:')
    );
    const textIsCoordinates =
      hasLocationAttachment &&
      message.msg.text &&
      /^-?\d+\.?\d*\s*,\s*-?\d+\.?\d*$/.test(message.msg.text.trim());
    // Keep the text interpolation itself whitespace-free: this element uses
    // pre-wrap, so formatter-inserted newlines become visible in screenshots.
    const messageText =
      this.searchHighlight && this.highlightMessageUuid === message.uuid
        ? this.highlightText(message.msg.text, this.searchHighlight)
        : message.msg.text;

    return html`
      <div class="bubble-wrap">
        ${this.showTimestamps
          ? html`<div class="popup" style="white-space: nowrap;">
              ${errorMessage
                ? html`<div
                    style="color: var(--color-error); margin-right: 1em;"
                  >
                    ${errorMessage}
                  </div>`
                : null}
              <temba-date
                value="${message.created_on.toISOString()}"
                display="datetime"
              ></temba-date>
              ${logsURL
                ? html`<a
                    class="log-link"
                    href="${logsURL}"
                    target="_blank"
                    rel="noopener noreferrer"
                    ><temba-icon name="log"></temba-icon
                  ></a>`
                : null}
 
              <div class="arrow"></div>
            </div>`
          : null}
        ${isDeleted
          ? html`<div class="bubble">
              ${name ? html`<div class="name">${name}</div>` : null}
              <div class="message-deleted">
                Message deleted by ${deletedByText}
              </div>
            </div>`
          : isEmpty
            ? html`<div class="bubble">
                ${name ? html`<div class="name">${name}</div>` : null}
                <div class="message-empty">Empty Message</div>
              </div>`
            : message.msg.text && !textIsCoordinates
              ? html`<div class="bubble">
                  ${name ? html`<div class="name">${name}</div>` : null}
                  <div class="message-text">${messageText}</div>
                </div>`
              : null}
 
        <div class="attachments">
          ${(message.msg.attachments || []).map(
            (attachment) =>
              html`<temba-thumbnail
                attachment="${attachmentAsString(attachment)}"
              ></temba-thumbnail>`
          )}
        </div>
      </div>
    `;
  }
 
  public reset() {
    this.resetGeneration++;
    this.typingTimeouts.forEach((timeout) => window.clearTimeout(timeout));
    this.typingTimeouts.clear();
    this.typingEvents.clear();
    this.msgMap.clear();
    this.metadataCache.clear();
    this.expandedEventChunks.clear();
    this.collapsingEventChunks.clear();
    this.messageGroups = [];
    this.hideBottomScroll = true;
    this.hideTopScroll = true;
    this.endOfHistory = false;
    this.oldestEventDate = null;
  }
 
  public setEndOfHistory(oldestDate: Date) {
    this.endOfHistory = true;
    this.oldestEventDate = oldestDate;
  }
 
  public render(): TemplateResult {
    return html` <div
      class="
        messages 
        ${this.hideBottomScroll ? 'scroll-at-bottom' : ''}
        ${this.hideTopScroll ? 'scroll-at-top' : ''}"
    >
      <div class="scroll" @scroll=${this.handleScroll}>
        ${this.messageGroups
          ? (() => {
              // bucket groups into calendar-day sections, oldest to
              // newest (high index to low index). Each section renders
              // with a sticky day marker that floats over its messages
              // while they scroll by.
              const sections: {
                key: string;
                label: string;
                groups: TemplateResult[];
              }[] = [];
              for (let idx = this.messageGroups.length - 1; idx >= 0; idx--) {
                const msgGroup = this.messageGroups[idx];
                const firstMsg = this.msgMap.get(msgGroup.messages[0]);
                const day = firstMsg?.created_on || new Date();
                const key = getDayKey(day);
                let section = sections[sections.length - 1];
                if (!section || section.key !== key) {
                  section = { key, label: getDayLabel(day), groups: [] };
                  sections.push(section);
                }
                // oldest first within the section — the section is a
                // normal top-down column, only the scroller reverses
                section.groups.push(this.renderMessageGroup(msgGroup));
              }
              // newest section first in DOM for the column-reverse scroller
              sections.reverse();
              return sections.map(
                (section) =>
                  html`<div class="day-section">
                    <div class="day-marker">
                      <div class="day">${section.label}</div>
                    </div>
                    ${section.groups}
                  </div>`
              );
            })()
          : null}
 
        <temba-loading
          class="${!this.fetching ? 'hidden' : ''}"
        ></temba-loading>
      </div>
      ${!this.hasFooter
        ? html`<div
            class="new-message-notification ${this.showNewMessageNotification
              ? 'visible'
              : ''}"
            @click=${this.handleNewMessageClick}
          >
            New Messages
          </div>`
        : null}
      <slot class="header" name="header"></slot>
      <slot class="footer" name="footer"></slot>
    </div>`;
  }
}