immer.umd.development.js 57.6 KB
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
(function (global, factory) {
	typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
	typeof define === 'function' && define.amd ? define(['exports'], factory) :
	(global = global || self, factory(global.immer = {}));
}(this, (function (exports) { 'use strict';

	var _ref;

	// Should be no imports here!
	// Some things that should be evaluated before all else...
	// We only want to know if non-polyfilled symbols are available
	var hasSymbol = typeof Symbol !== "undefined" && typeof
	/*#__PURE__*/
	Symbol("x") === "symbol";
	var hasMap = typeof Map !== "undefined";
	var hasSet = typeof Set !== "undefined";
	var hasProxies = typeof Proxy !== "undefined" && typeof Proxy.revocable !== "undefined" && typeof Reflect !== "undefined";
	/**
	 * The sentinel value returned by producers to replace the draft with undefined.
	 */

	var NOTHING = hasSymbol ?
	/*#__PURE__*/
	Symbol.for("immer-nothing") : (_ref = {}, _ref["immer-nothing"] = true, _ref);
	/**
	 * To let Immer treat your class instances as plain immutable objects
	 * (albeit with a custom prototype), you must define either an instance property
	 * or a static property on each of your custom classes.
	 *
	 * Otherwise, your class instance will never be drafted, which means it won't be
	 * safe to mutate in a produce callback.
	 */

	var DRAFTABLE = hasSymbol ?
	/*#__PURE__*/
	Symbol.for("immer-draftable") : "__$immer_draftable";
	var DRAFT_STATE = hasSymbol ?
	/*#__PURE__*/
	Symbol.for("immer-state") : "__$immer_state"; // Even a polyfilled Symbol might provide Symbol.iterator

	var iteratorSymbol = typeof Symbol != "undefined" && Symbol.iterator || "@@iterator";

	var errors = {
	  0: "Illegal state",
	  1: "Immer drafts cannot have computed properties",
	  2: "This object has been frozen and should not be mutated",
	  3: function _(data) {
	    return "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? " + data;
	  },
	  4: "An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.",
	  5: "Immer forbids circular references",
	  6: "The first or second argument to `produce` must be a function",
	  7: "The third argument to `produce` must be a function or undefined",
	  8: "First argument to `createDraft` must be a plain object, an array, or an immerable object",
	  9: "First argument to `finishDraft` must be a draft returned by `createDraft`",
	  10: "The given draft is already finalized",
	  11: "Object.defineProperty() cannot be used on an Immer draft",
	  12: "Object.setPrototypeOf() cannot be used on an Immer draft",
	  13: "Immer only supports deleting array indices",
	  14: "Immer only supports setting array indices and the 'length' property",
	  15: function _(path) {
	    return "Cannot apply patch, path doesn't resolve: " + path;
	  },
	  16: 'Sets cannot have "replace" patches.',
	  17: function _(op) {
	    return "Unsupported patch operation: " + op;
	  },
	  18: function _(plugin) {
	    return "The plugin for '" + plugin + "' has not been loaded into Immer. To enable the plugin, import and call `enable" + plugin + "()` when initializing your application.";
	  },
	  19: "plugin not loaded",
	  20: "Cannot use proxies if Proxy, Proxy.revocable or Reflect are not available",
	  21: function _(thing) {
	    return "produce can only be called on things that are draftable: plain objects, arrays, Map, Set or classes that are marked with '[immerable]: true'. Got '" + thing + "'";
	  },
	  22: function _(thing) {
	    return "'current' expects a draft, got: " + thing;
	  },
	  23: function _(thing) {
	    return "'original' expects a draft, got: " + thing;
	  }
	};
	function die(error) {
	  for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
	    args[_key - 1] = arguments[_key];
	  }

	  {
	    var e = errors[error];
	    var msg = !e ? "unknown error nr: " + error : typeof e === "function" ? e.apply(null, args) : e;
	    throw new Error("[Immer] " + msg);
	  }
	}

	var ArchtypeObject = 0;
	var ArchtypeArray = 1;
	var ArchtypeMap = 2;
	var ArchtypeSet = 3;
	var ProxyTypeProxyObject = 0;
	var ProxyTypeProxyArray = 1;
	var ProxyTypeES5Object = 4;
	var ProxyTypeES5Array = 5;
	var ProxyTypeMap = 2;
	var ProxyTypeSet = 3;

	/** Returns true if the given value is an Immer draft */

	/*#__PURE__*/

	function isDraft(value) {
	  return !!value && !!value[DRAFT_STATE];
	}
	/** Returns true if the given value can be drafted by Immer */

	/*#__PURE__*/

	function isDraftable(value) {
	  if (!value) return false;
	  return isPlainObject(value) || Array.isArray(value) || !!value[DRAFTABLE] || !!value.constructor[DRAFTABLE] || isMap(value) || isSet(value);
	}
	/*#__PURE__*/

	function isPlainObject(value) {
	  if (!value || typeof value !== "object") return false;
	  var proto = Object.getPrototypeOf(value);
	  return !proto || proto === Object.prototype;
	}
	function original(value) {
	  if (!isDraft(value)) die(23, value);
	  return value[DRAFT_STATE].base_;
	}
	/*#__PURE__*/

	var ownKeys = typeof Reflect !== "undefined" && Reflect.ownKeys ? Reflect.ownKeys : typeof Object.getOwnPropertySymbols !== "undefined" ? function (obj) {
	  return Object.getOwnPropertyNames(obj).concat(Object.getOwnPropertySymbols(obj));
	} :
	/* istanbul ignore next */
	Object.getOwnPropertyNames;
	var getOwnPropertyDescriptors = Object.getOwnPropertyDescriptors || function getOwnPropertyDescriptors(target) {
	  // Polyfill needed for Hermes and IE, see https://github.com/facebook/hermes/issues/274
	  var res = {};
	  ownKeys(target).forEach(function (key) {
	    res[key] = Object.getOwnPropertyDescriptor(target, key);
	  });
	  return res;
	};
	function each(obj, iter, enumerableOnly) {
	  if (enumerableOnly === void 0) {
	    enumerableOnly = false;
	  }

	  if (getArchtype(obj) === ArchtypeObject) {
	    (enumerableOnly ? Object.keys : ownKeys)(obj).forEach(function (key) {
	      if (!enumerableOnly || typeof key !== "symbol") iter(key, obj[key], obj);
	    });
	  } else {
	    obj.forEach(function (entry, index) {
	      return iter(index, entry, obj);
	    });
	  }
	}
	/*#__PURE__*/

	function getArchtype(thing) {
	  /* istanbul ignore next */
	  var state = thing[DRAFT_STATE];
	  return state ? state.type_ > 3 ? state.type_ - 4 // cause Object and Array map back from 4 and 5
	  : state.type_ // others are the same
	  : Array.isArray(thing) ? ArchtypeArray : isMap(thing) ? ArchtypeMap : isSet(thing) ? ArchtypeSet : ArchtypeObject;
	}
	/*#__PURE__*/

	function has(thing, prop) {
	  return getArchtype(thing) === ArchtypeMap ? thing.has(prop) : Object.prototype.hasOwnProperty.call(thing, prop);
	}
	/*#__PURE__*/

	function get(thing, prop) {
	  // @ts-ignore
	  return getArchtype(thing) === ArchtypeMap ? thing.get(prop) : thing[prop];
	}
	/*#__PURE__*/

	function set(thing, propOrOldValue, value) {
	  var t = getArchtype(thing);
	  if (t === ArchtypeMap) thing.set(propOrOldValue, value);else if (t === ArchtypeSet) {
	    thing.delete(propOrOldValue);
	    thing.add(value);
	  } else thing[propOrOldValue] = value;
	}
	/*#__PURE__*/

	function is(x, y) {
	  // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js
	  if (x === y) {
	    return x !== 0 || 1 / x === 1 / y;
	  } else {
	    return x !== x && y !== y;
	  }
	}
	/*#__PURE__*/

	function isMap(target) {
	  return hasMap && target instanceof Map;
	}
	/*#__PURE__*/

	function isSet(target) {
	  return hasSet && target instanceof Set;
	}
	/*#__PURE__*/

	function latest(state) {
	  return state.copy_ || state.base_;
	}
	/*#__PURE__*/

	function shallowCopy(base) {
	  if (Array.isArray(base)) return Array.prototype.slice.call(base);
	  var descriptors = getOwnPropertyDescriptors(base);
	  delete descriptors[DRAFT_STATE];
	  var keys = ownKeys(descriptors);

	  for (var i = 0; i < keys.length; i++) {
	    var key = keys[i];
	    var desc = descriptors[key];

	    if (desc.writable === false) {
	      desc.writable = true;
	      desc.configurable = true;
	    } // like object.assign, we will read any _own_, get/set accessors. This helps in dealing
	    // with libraries that trap values, like mobx or vue
	    // unlike object.assign, non-enumerables will be copied as well


	    if (desc.get || desc.set) descriptors[key] = {
	      configurable: true,
	      writable: true,
	      enumerable: desc.enumerable,
	      value: base[key]
	    };
	  }

	  return Object.create(Object.getPrototypeOf(base), descriptors);
	}
	function freeze(obj, deep) {
	  if (isFrozen(obj) || isDraft(obj) || !isDraftable(obj)) return;

	  if (getArchtype(obj) > 1
	  /* Map or Set */
	  ) {
	      obj.set = obj.add = obj.clear = obj.delete = dontMutateFrozenCollections;
	    }

	  Object.freeze(obj);
	  if (deep) each(obj, function (key, value) {
	    return freeze(value, true);
	  }, true);
	}

	function dontMutateFrozenCollections() {
	  die(2);
	}

	function isFrozen(obj) {
	  if (obj == null || typeof obj !== "object") return true; // See #600, IE dies on non-objects in Object.isFrozen

	  return Object.isFrozen(obj);
	}

	/** Plugin utilities */

	var plugins = {};
	function getPlugin(pluginKey) {
	  var plugin = plugins[pluginKey];

	  if (!plugin) {
	    die( 18 , pluginKey);
	  } // @ts-ignore


	  return plugin;
	}
	function loadPlugin(pluginKey, implementation) {
	  plugins[pluginKey] = implementation;
	}

	var currentScope;
	function getCurrentScope() {
	  if ( !currentScope) die(0);
	  return currentScope;
	}

	function createScope(parent_, immer_) {
	  return {
	    drafts_: [],
	    parent_: parent_,
	    immer_: immer_,
	    // Whenever the modified draft contains a draft from another scope, we
	    // need to prevent auto-freezing so the unowned draft can be finalized.
	    canAutoFreeze_: true,
	    unfinalizedDrafts_: 0
	  };
	}

	function usePatchesInScope(scope, patchListener) {
	  if (patchListener) {
	    getPlugin("Patches"); // assert we have the plugin

	    scope.patches_ = [];
	    scope.inversePatches_ = [];
	    scope.patchListener_ = patchListener;
	  }
	}
	function revokeScope(scope) {
	  leaveScope(scope);
	  scope.drafts_.forEach(revokeDraft); // @ts-ignore

	  scope.drafts_ = null;
	}
	function leaveScope(scope) {
	  if (scope === currentScope) {
	    currentScope = scope.parent_;
	  }
	}
	function enterScope(immer) {
	  return currentScope = createScope(currentScope, immer);
	}

	function revokeDraft(draft) {
	  var state = draft[DRAFT_STATE];
	  if (state.type_ === ProxyTypeProxyObject || state.type_ === ProxyTypeProxyArray) state.revoke_();else state.revoked_ = true;
	}

	function processResult(result, scope) {
	  scope.unfinalizedDrafts_ = scope.drafts_.length;
	  var baseDraft = scope.drafts_[0];
	  var isReplaced = result !== undefined && result !== baseDraft;
	  if (!scope.immer_.useProxies_) getPlugin("ES5").willFinalizeES5_(scope, result, isReplaced);

	  if (isReplaced) {
	    if (baseDraft[DRAFT_STATE].modified_) {
	      revokeScope(scope);
	      die(4);
	    }

	    if (isDraftable(result)) {
	      // Finalize the result in case it contains (or is) a subset of the draft.
	      result = finalize(scope, result);
	      if (!scope.parent_) maybeFreeze(scope, result);
	    }

	    if (scope.patches_) {
	      getPlugin("Patches").generateReplacementPatches_(baseDraft[DRAFT_STATE], result, scope.patches_, scope.inversePatches_);
	    }
	  } else {
	    // Finalize the base draft.
	    result = finalize(scope, baseDraft, []);
	  }

	  revokeScope(scope);

	  if (scope.patches_) {
	    scope.patchListener_(scope.patches_, scope.inversePatches_);
	  }

	  return result !== NOTHING ? result : undefined;
	}

	function finalize(rootScope, value, path) {
	  // Don't recurse in tho recursive data structures
	  if (isFrozen(value)) return value;
	  var state = value[DRAFT_STATE]; // A plain object, might need freezing, might contain drafts

	  if (!state) {
	    each(value, function (key, childValue) {
	      return finalizeProperty(rootScope, state, value, key, childValue, path);
	    }, true // See #590, don't recurse into non-enumarable of non drafted objects
	    );
	    return value;
	  } // Never finalize drafts owned by another scope.


	  if (state.scope_ !== rootScope) return value; // Unmodified draft, return the (frozen) original

	  if (!state.modified_) {
	    maybeFreeze(rootScope, state.base_, true);
	    return state.base_;
	  } // Not finalized yet, let's do that now


	  if (!state.finalized_) {
	    state.finalized_ = true;
	    state.scope_.unfinalizedDrafts_--;
	    var result = // For ES5, create a good copy from the draft first, with added keys and without deleted keys.
	    state.type_ === ProxyTypeES5Object || state.type_ === ProxyTypeES5Array ? state.copy_ = shallowCopy(state.draft_) : state.copy_; // Finalize all children of the copy
	    // For sets we clone before iterating, otherwise we can get in endless loop due to modifying during iteration, see #628
	    // Although the original test case doesn't seem valid anyway, so if this in the way we can turn the next line
	    // back to each(result, ....)

	    each(state.type_ === ProxyTypeSet ? new Set(result) : result, function (key, childValue) {
	      return finalizeProperty(rootScope, state, result, key, childValue, path);
	    }); // everything inside is frozen, we can freeze here

	    maybeFreeze(rootScope, result, false); // first time finalizing, let's create those patches

	    if (path && rootScope.patches_) {
	      getPlugin("Patches").generatePatches_(state, path, rootScope.patches_, rootScope.inversePatches_);
	    }
	  }

	  return state.copy_;
	}

	function finalizeProperty(rootScope, parentState, targetObject, prop, childValue, rootPath) {
	  if ( childValue === targetObject) die(5);

	  if (isDraft(childValue)) {
	    var path = rootPath && parentState && parentState.type_ !== ProxyTypeSet && // Set objects are atomic since they have no keys.
	    !has(parentState.assigned_, prop) // Skip deep patches for assigned keys.
	    ? rootPath.concat(prop) : undefined; // Drafts owned by `scope` are finalized here.

	    var res = finalize(rootScope, childValue, path);
	    set(targetObject, prop, res); // Drafts from another scope must prevented to be frozen
	    // if we got a draft back from finalize, we're in a nested produce and shouldn't freeze

	    if (isDraft(res)) {
	      rootScope.canAutoFreeze_ = false;
	    } else return;
	  } // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.


	  if (isDraftable(childValue) && !isFrozen(childValue)) {
	    if (!rootScope.immer_.autoFreeze_ && rootScope.unfinalizedDrafts_ < 1) {
	      // optimization: if an object is not a draft, and we don't have to
	      // deepfreeze everything, and we are sure that no drafts are left in the remaining object
	      // cause we saw and finalized all drafts already; we can stop visiting the rest of the tree.
	      // This benefits especially adding large data tree's without further processing.
	      // See add-data.js perf test
	      return;
	    }

	    finalize(rootScope, childValue); // immer deep freezes plain objects, so if there is no parent state, we freeze as well

	    if (!parentState || !parentState.scope_.parent_) maybeFreeze(rootScope, childValue);
	  }
	}

	function maybeFreeze(scope, value, deep) {
	  if (deep === void 0) {
	    deep = false;
	  }

	  if (scope.immer_.autoFreeze_ && scope.canAutoFreeze_) {
	    freeze(value, deep);
	  }
	}

	/**
	 * Returns a new draft of the `base` object.
	 *
	 * The second argument is the parent draft-state (used internally).
	 */

	function createProxyProxy(base, parent) {
	  var isArray = Array.isArray(base);
	  var state = {
	    type_: isArray ? ProxyTypeProxyArray : ProxyTypeProxyObject,
	    // Track which produce call this is associated with.
	    scope_: parent ? parent.scope_ : getCurrentScope(),
	    // True for both shallow and deep changes.
	    modified_: false,
	    // Used during finalization.
	    finalized_: false,
	    // Track which properties have been assigned (true) or deleted (false).
	    assigned_: {},
	    // The parent draft state.
	    parent_: parent,
	    // The base state.
	    base_: base,
	    // The base proxy.
	    draft_: null,
	    // The base copy with any updated values.
	    copy_: null,
	    // Called by the `produce` function.
	    revoke_: null,
	    isManual_: false
	  }; // the traps must target something, a bit like the 'real' base.
	  // but also, we need to be able to determine from the target what the relevant state is
	  // (to avoid creating traps per instance to capture the state in closure,
	  // and to avoid creating weird hidden properties as well)
	  // So the trick is to use 'state' as the actual 'target'! (and make sure we intercept everything)
	  // Note that in the case of an array, we put the state in an array to have better Reflect defaults ootb

	  var target = state;
	  var traps = objectTraps;

	  if (isArray) {
	    target = [state];
	    traps = arrayTraps;
	  }

	  var _Proxy$revocable = Proxy.revocable(target, traps),
	      revoke = _Proxy$revocable.revoke,
	      proxy = _Proxy$revocable.proxy;

	  state.draft_ = proxy;
	  state.revoke_ = revoke;
	  return proxy;
	}
	/**
	 * Object drafts
	 */

	var objectTraps = {
	  get: function get(state, prop) {
	    if (prop === DRAFT_STATE) return state;
	    var source = latest(state);

	    if (!has(source, prop)) {
	      // non-existing or non-own property...
	      return readPropFromProto(state, source, prop);
	    }

	    var value = source[prop];

	    if (state.finalized_ || !isDraftable(value)) {
	      return value;
	    } // Check for existing draft in modified state.
	    // Assigned values are never drafted. This catches any drafts we created, too.


	    if (value === peek(state.base_, prop)) {
	      prepareCopy(state);
	      return state.copy_[prop] = createProxy(state.scope_.immer_, value, state);
	    }

	    return value;
	  },
	  has: function has(state, prop) {
	    return prop in latest(state);
	  },
	  ownKeys: function ownKeys(state) {
	    return Reflect.ownKeys(latest(state));
	  },
	  set: function set(state, prop
	  /* strictly not, but helps TS */
	  , value) {
	    var desc = getDescriptorFromProto(latest(state), prop);

	    if (desc === null || desc === void 0 ? void 0 : desc.set) {
	      // special case: if this write is captured by a setter, we have
	      // to trigger it with the correct context
	      desc.set.call(state.draft_, value);
	      return true;
	    }

	    state.assigned_[prop] = true;

	    if (!state.modified_) {
	      // the last check is because we need to be able to distinguish setting a non-existig to undefined (which is a change)
	      // from setting an existing property with value undefined to undefined (which is not a change)
	      if (is(value, peek(latest(state), prop)) && (value !== undefined || has(state.base_, prop))) return true;
	      prepareCopy(state);
	      markChanged(state);
	    } // @ts-ignore


	    state.copy_[prop] = value;
	    return true;
	  },
	  deleteProperty: function deleteProperty(state, prop) {
	    // The `undefined` check is a fast path for pre-existing keys.
	    if (peek(state.base_, prop) !== undefined || prop in state.base_) {
	      state.assigned_[prop] = false;
	      prepareCopy(state);
	      markChanged(state);
	    } else {
	      // if an originally not assigned property was deleted
	      delete state.assigned_[prop];
	    } // @ts-ignore


	    if (state.copy_) delete state.copy_[prop];
	    return true;
	  },
	  // Note: We never coerce `desc.value` into an Immer draft, because we can't make
	  // the same guarantee in ES5 mode.
	  getOwnPropertyDescriptor: function getOwnPropertyDescriptor(state, prop) {
	    var owner = latest(state);
	    var desc = Reflect.getOwnPropertyDescriptor(owner, prop);
	    if (!desc) return desc;
	    return {
	      writable: true,
	      configurable: state.type_ !== ProxyTypeProxyArray || prop !== "length",
	      enumerable: desc.enumerable,
	      value: owner[prop]
	    };
	  },
	  defineProperty: function defineProperty() {
	    die(11);
	  },
	  getPrototypeOf: function getPrototypeOf(state) {
	    return Object.getPrototypeOf(state.base_);
	  },
	  setPrototypeOf: function setPrototypeOf() {
	    die(12);
	  }
	};
	/**
	 * Array drafts
	 */

	var arrayTraps = {};
	each(objectTraps, function (key, fn) {
	  // @ts-ignore
	  arrayTraps[key] = function () {
	    arguments[0] = arguments[0][0];
	    return fn.apply(this, arguments);
	  };
	});

	arrayTraps.deleteProperty = function (state, prop) {
	  if ( isNaN(parseInt(prop))) die(13);
	  return objectTraps.deleteProperty.call(this, state[0], prop);
	};

	arrayTraps.set = function (state, prop, value) {
	  if ( prop !== "length" && isNaN(parseInt(prop))) die(14);
	  return objectTraps.set.call(this, state[0], prop, value, state[0]);
	}; // Access a property without creating an Immer draft.


	function peek(draft, prop) {
	  var state = draft[DRAFT_STATE];
	  var source = state ? latest(state) : draft;
	  return source[prop];
	}

	function readPropFromProto(state, source, prop) {
	  var _desc$get;

	  var desc = getDescriptorFromProto(source, prop);
	  return desc ? "value" in desc ? desc.value : // This is a very special case, if the prop is a getter defined by the
	  // prototype, we should invoke it with the draft as context!
	  (_desc$get = desc.get) === null || _desc$get === void 0 ? void 0 : _desc$get.call(state.draft_) : undefined;
	}

	function getDescriptorFromProto(source, prop) {
	  // 'in' checks proto!
	  if (!(prop in source)) return undefined;
	  var proto = Object.getPrototypeOf(source);

	  while (proto) {
	    var desc = Object.getOwnPropertyDescriptor(proto, prop);
	    if (desc) return desc;
	    proto = Object.getPrototypeOf(proto);
	  }

	  return undefined;
	}

	function markChanged(state) {
	  if (!state.modified_) {
	    state.modified_ = true;

	    if (state.parent_) {
	      markChanged(state.parent_);
	    }
	  }
	}
	function prepareCopy(state) {
	  if (!state.copy_) {
	    state.copy_ = shallowCopy(state.base_);
	  }
	}

	var Immer =
	/*#__PURE__*/
	function () {
	  function Immer(config) {
	    this.useProxies_ = hasProxies;
	    this.autoFreeze_ =  true
	    /* istanbul ignore next */
	    ;
	    if (typeof (config === null || config === void 0 ? void 0 : config.useProxies) === "boolean") this.setUseProxies(config.useProxies);
	    if (typeof (config === null || config === void 0 ? void 0 : config.autoFreeze) === "boolean") this.setAutoFreeze(config.autoFreeze);
	    this.produce = this.produce.bind(this);
	    this.produceWithPatches = this.produceWithPatches.bind(this);
	  }
	  /**
	   * The `produce` function takes a value and a "recipe function" (whose
	   * return value often depends on the base state). The recipe function is
	   * free to mutate its first argument however it wants. All mutations are
	   * only ever applied to a __copy__ of the base state.
	   *
	   * Pass only a function to create a "curried producer" which relieves you
	   * from passing the recipe function every time.
	   *
	   * Only plain objects and arrays are made mutable. All other objects are
	   * considered uncopyable.
	   *
	   * Note: This function is __bound__ to its `Immer` instance.
	   *
	   * @param {any} base - the initial state
	   * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified
	   * @param {Function} patchListener - optional function that will be called with all the patches produced here
	   * @returns {any} a new state, or the initial state if nothing was modified
	   */


	  var _proto = Immer.prototype;

	  _proto.produce = function produce(base, recipe, patchListener) {
	    // curried invocation
	    if (typeof base === "function" && typeof recipe !== "function") {
	      var defaultBase = recipe;
	      recipe = base;
	      var self = this;
	      return function curriedProduce(base) {
	        var _this = this;

	        if (base === void 0) {
	          base = defaultBase;
	        }

	        for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
	          args[_key - 1] = arguments[_key];
	        }

	        return self.produce(base, function (draft) {
	          var _recipe;

	          return (_recipe = recipe).call.apply(_recipe, [_this, draft].concat(args));
	        }); // prettier-ignore
	      };
	    }

	    if (typeof recipe !== "function") die(6);
	    if (patchListener !== undefined && typeof patchListener !== "function") die(7);
	    var result; // Only plain objects, arrays, and "immerable classes" are drafted.

	    if (isDraftable(base)) {
	      var scope = enterScope(this);
	      var proxy = createProxy(this, base, undefined);
	      var hasError = true;

	      try {
	        result = recipe(proxy);
	        hasError = false;
	      } finally {
	        // finally instead of catch + rethrow better preserves original stack
	        if (hasError) revokeScope(scope);else leaveScope(scope);
	      }

	      if (typeof Promise !== "undefined" && result instanceof Promise) {
	        return result.then(function (result) {
	          usePatchesInScope(scope, patchListener);
	          return processResult(result, scope);
	        }, function (error) {
	          revokeScope(scope);
	          throw error;
	        });
	      }

	      usePatchesInScope(scope, patchListener);
	      return processResult(result, scope);
	    } else if (!base || typeof base !== "object") {
	      result = recipe(base);
	      if (result === NOTHING) return undefined;
	      if (result === undefined) result = base;
	      if (this.autoFreeze_) freeze(result, true);
	      return result;
	    } else die(21, base);
	  };

	  _proto.produceWithPatches = function produceWithPatches(arg1, arg2, arg3) {
	    var _this2 = this;

	    if (typeof arg1 === "function") {
	      return function (state) {
	        for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
	          args[_key2 - 1] = arguments[_key2];
	        }

	        return _this2.produceWithPatches(state, function (draft) {
	          return arg1.apply(void 0, [draft].concat(args));
	        });
	      };
	    }

	    var patches, inversePatches;
	    var nextState = this.produce(arg1, arg2, function (p, ip) {
	      patches = p;
	      inversePatches = ip;
	    });
	    return [nextState, patches, inversePatches];
	  };

	  _proto.createDraft = function createDraft(base) {
	    if (!isDraftable(base)) die(8);
	    if (isDraft(base)) base = current(base);
	    var scope = enterScope(this);
	    var proxy = createProxy(this, base, undefined);
	    proxy[DRAFT_STATE].isManual_ = true;
	    leaveScope(scope);
	    return proxy;
	  };

	  _proto.finishDraft = function finishDraft(draft, patchListener) {
	    var state = draft && draft[DRAFT_STATE];

	    {
	      if (!state || !state.isManual_) die(9);
	      if (state.finalized_) die(10);
	    }

	    var scope = state.scope_;
	    usePatchesInScope(scope, patchListener);
	    return processResult(undefined, scope);
	  }
	  /**
	   * Pass true to automatically freeze all copies created by Immer.
	   *
	   * By default, auto-freezing is disabled in production.
	   */
	  ;

	  _proto.setAutoFreeze = function setAutoFreeze(value) {
	    this.autoFreeze_ = value;
	  }
	  /**
	   * Pass true to use the ES2015 `Proxy` class when creating drafts, which is
	   * always faster than using ES5 proxies.
	   *
	   * By default, feature detection is used, so calling this is rarely necessary.
	   */
	  ;

	  _proto.setUseProxies = function setUseProxies(value) {
	    if (value && !hasProxies) {
	      die(20);
	    }

	    this.useProxies_ = value;
	  };

	  _proto.applyPatches = function applyPatches(base, patches) {
	    // If a patch replaces the entire state, take that replacement as base
	    // before applying patches
	    var i;

	    for (i = patches.length - 1; i >= 0; i--) {
	      var patch = patches[i];

	      if (patch.path.length === 0 && patch.op === "replace") {
	        base = patch.value;
	        break;
	      }
	    }

	    var applyPatchesImpl = getPlugin("Patches").applyPatches_;

	    if (isDraft(base)) {
	      // N.B: never hits if some patch a replacement, patches are never drafts
	      return applyPatchesImpl(base, patches);
	    } // Otherwise, produce a copy of the base state.


	    return this.produce(base, function (draft) {
	      return applyPatchesImpl(draft, patches.slice(i + 1));
	    });
	  };

	  return Immer;
	}();
	function createProxy(immer, value, parent) {
	  // precondition: createProxy should be guarded by isDraftable, so we know we can safely draft
	  var draft = isMap(value) ? getPlugin("MapSet").proxyMap_(value, parent) : isSet(value) ? getPlugin("MapSet").proxySet_(value, parent) : immer.useProxies_ ? createProxyProxy(value, parent) : getPlugin("ES5").createES5Proxy_(value, parent);
	  var scope = parent ? parent.scope_ : getCurrentScope();
	  scope.drafts_.push(draft);
	  return draft;
	}

	function current(value) {
	  if (!isDraft(value)) die(22, value);
	  return currentImpl(value);
	}

	function currentImpl(value) {
	  if (!isDraftable(value)) return value;
	  var state = value[DRAFT_STATE];
	  var copy;
	  var archType = getArchtype(value);

	  if (state) {
	    if (!state.modified_ && (state.type_ < 4 || !getPlugin("ES5").hasChanges_(state))) return state.base_; // Optimization: avoid generating new drafts during copying

	    state.finalized_ = true;
	    copy = copyHelper(value, archType);
	    state.finalized_ = false;
	  } else {
	    copy = copyHelper(value, archType);
	  }

	  each(copy, function (key, childValue) {
	    if (state && get(state.base_, key) === childValue) return; // no need to copy or search in something that didn't change

	    set(copy, key, currentImpl(childValue));
	  }); // In the future, we might consider freezing here, based on the current settings

	  return archType === ArchtypeSet ? new Set(copy) : copy;
	}

	function copyHelper(value, archType) {
	  // creates a shallow copy, even if it is a map or set
	  switch (archType) {
	    case ArchtypeMap:
	      return new Map(value);

	    case ArchtypeSet:
	      // Set will be cloned as array temporarily, so that we can replace individual items
	      return Array.from(value);
	  }

	  return shallowCopy(value);
	}

	function enableES5() {
	  function willFinalizeES5_(scope, result, isReplaced) {
	    if (!isReplaced) {
	      if (scope.patches_) {
	        markChangesRecursively(scope.drafts_[0]);
	      } // This is faster when we don't care about which attributes changed.


	      markChangesSweep(scope.drafts_);
	    } // When a child draft is returned, look for changes.
	    else if (isDraft(result) && result[DRAFT_STATE].scope_ === scope) {
	        markChangesSweep(scope.drafts_);
	      }
	  }

	  function createES5Draft(isArray, base) {
	    if (isArray) {
	      var draft = new Array(base.length);

	      for (var i = 0; i < base.length; i++) {
	        Object.defineProperty(draft, "" + i, proxyProperty(i, true));
	      }

	      return draft;
	    } else {
	      var _descriptors = getOwnPropertyDescriptors(base);

	      delete _descriptors[DRAFT_STATE];
	      var keys = ownKeys(_descriptors);

	      for (var _i = 0; _i < keys.length; _i++) {
	        var key = keys[_i];
	        _descriptors[key] = proxyProperty(key, isArray || !!_descriptors[key].enumerable);
	      }

	      return Object.create(Object.getPrototypeOf(base), _descriptors);
	    }
	  }

	  function createES5Proxy_(base, parent) {
	    var isArray = Array.isArray(base);
	    var draft = createES5Draft(isArray, base);
	    var state = {
	      type_: isArray ? ProxyTypeES5Array : ProxyTypeES5Object,
	      scope_: parent ? parent.scope_ : getCurrentScope(),
	      modified_: false,
	      finalized_: false,
	      assigned_: {},
	      parent_: parent,
	      // base is the object we are drafting
	      base_: base,
	      // draft is the draft object itself, that traps all reads and reads from either the base (if unmodified) or copy (if modified)
	      draft_: draft,
	      copy_: null,
	      revoked_: false,
	      isManual_: false
	    };
	    Object.defineProperty(draft, DRAFT_STATE, {
	      value: state,
	      // enumerable: false <- the default
	      writable: true
	    });
	    return draft;
	  } // property descriptors are recycled to make sure we don't create a get and set closure per property,
	  // but share them all instead


	  var descriptors = {};

	  function proxyProperty(prop, enumerable) {
	    var desc = descriptors[prop];

	    if (desc) {
	      desc.enumerable = enumerable;
	    } else {
	      descriptors[prop] = desc = {
	        configurable: true,
	        enumerable: enumerable,
	        get: function get() {
	          var state = this[DRAFT_STATE];
	          assertUnrevoked(state); // @ts-ignore

	          return objectTraps.get(state, prop);
	        },
	        set: function set(value) {
	          var state = this[DRAFT_STATE];
	          assertUnrevoked(state); // @ts-ignore

	          objectTraps.set(state, prop, value);
	        }
	      };
	    }

	    return desc;
	  } // This looks expensive, but only proxies are visited, and only objects without known changes are scanned.


	  function markChangesSweep(drafts) {
	    // The natural order of drafts in the `scope` array is based on when they
	    // were accessed. By processing drafts in reverse natural order, we have a
	    // better chance of processing leaf nodes first. When a leaf node is known to
	    // have changed, we can avoid any traversal of its ancestor nodes.
	    for (var i = drafts.length - 1; i >= 0; i--) {
	      var state = drafts[i][DRAFT_STATE];

	      if (!state.modified_) {
	        switch (state.type_) {
	          case ProxyTypeES5Array:
	            if (hasArrayChanges(state)) markChanged(state);
	            break;

	          case ProxyTypeES5Object:
	            if (hasObjectChanges(state)) markChanged(state);
	            break;
	        }
	      }
	    }
	  }

	  function markChangesRecursively(object) {
	    if (!object || typeof object !== "object") return;
	    var state = object[DRAFT_STATE];
	    if (!state) return;
	    var base_ = state.base_,
	        draft_ = state.draft_,
	        assigned_ = state.assigned_,
	        type_ = state.type_;

	    if (type_ === ProxyTypeES5Object) {
	      // Look for added keys.
	      // probably there is a faster way to detect changes, as sweep + recurse seems to do some
	      // unnecessary work.
	      // also: probably we can store the information we detect here, to speed up tree finalization!
	      each(draft_, function (key) {
	        if (key === DRAFT_STATE) return; // The `undefined` check is a fast path for pre-existing keys.

	        if (base_[key] === undefined && !has(base_, key)) {
	          assigned_[key] = true;
	          markChanged(state);
	        } else if (!assigned_[key]) {
	          // Only untouched properties trigger recursion.
	          markChangesRecursively(draft_[key]);
	        }
	      }); // Look for removed keys.

	      each(base_, function (key) {
	        // The `undefined` check is a fast path for pre-existing keys.
	        if (draft_[key] === undefined && !has(draft_, key)) {
	          assigned_[key] = false;
	          markChanged(state);
	        }
	      });
	    } else if (type_ === ProxyTypeES5Array) {
	      if (hasArrayChanges(state)) {
	        markChanged(state);
	        assigned_.length = true;
	      }

	      if (draft_.length < base_.length) {
	        for (var i = draft_.length; i < base_.length; i++) {
	          assigned_[i] = false;
	        }
	      } else {
	        for (var _i2 = base_.length; _i2 < draft_.length; _i2++) {
	          assigned_[_i2] = true;
	        }
	      } // Minimum count is enough, the other parts has been processed.


	      var min = Math.min(draft_.length, base_.length);

	      for (var _i3 = 0; _i3 < min; _i3++) {
	        // Only untouched indices trigger recursion.
	        if (assigned_[_i3] === undefined) markChangesRecursively(draft_[_i3]);
	      }
	    }
	  }

	  function hasObjectChanges(state) {
	    var base_ = state.base_,
	        draft_ = state.draft_; // Search for added keys and changed keys. Start at the back, because
	    // non-numeric keys are ordered by time of definition on the object.

	    var keys = ownKeys(draft_);

	    for (var i = keys.length - 1; i >= 0; i--) {
	      var key = keys[i];
	      if (key === DRAFT_STATE) continue;
	      var baseValue = base_[key]; // The `undefined` check is a fast path for pre-existing keys.

	      if (baseValue === undefined && !has(base_, key)) {
	        return true;
	      } // Once a base key is deleted, future changes go undetected, because its
	      // descriptor is erased. This branch detects any missed changes.
	      else {
	          var value = draft_[key];

	          var _state = value && value[DRAFT_STATE];

	          if (_state ? _state.base_ !== baseValue : !is(value, baseValue)) {
	            return true;
	          }
	        }
	    } // At this point, no keys were added or changed.
	    // Compare key count to determine if keys were deleted.


	    var baseIsDraft = !!base_[DRAFT_STATE];
	    return keys.length !== ownKeys(base_).length + (baseIsDraft ? 0 : 1); // + 1 to correct for DRAFT_STATE
	  }

	  function hasArrayChanges(state) {
	    var draft_ = state.draft_;
	    if (draft_.length !== state.base_.length) return true; // See #116
	    // If we first shorten the length, our array interceptors will be removed.
	    // If after that new items are added, result in the same original length,
	    // those last items will have no intercepting property.
	    // So if there is no own descriptor on the last position, we know that items were removed and added
	    // N.B.: splice, unshift, etc only shift values around, but not prop descriptors, so we only have to check
	    // the last one

	    var descriptor = Object.getOwnPropertyDescriptor(draft_, draft_.length - 1); // descriptor can be null, but only for newly created sparse arrays, eg. new Array(10)

	    if (descriptor && !descriptor.get) return true; // For all other cases, we don't have to compare, as they would have been picked up by the index setters

	    return false;
	  }

	  function hasChanges_(state) {
	    return state.type_ === ProxyTypeES5Object ? hasObjectChanges(state) : hasArrayChanges(state);
	  }

	  function assertUnrevoked(state
	  /*ES5State | MapState | SetState*/
	  ) {
	    if (state.revoked_) die(3, JSON.stringify(latest(state)));
	  }

	  loadPlugin("ES5", {
	    createES5Proxy_: createES5Proxy_,
	    willFinalizeES5_: willFinalizeES5_,
	    hasChanges_: hasChanges_
	  });
	}

	function enablePatches() {
	  var REPLACE = "replace";
	  var ADD = "add";
	  var REMOVE = "remove";

	  function generatePatches_(state, basePath, patches, inversePatches) {
	    switch (state.type_) {
	      case ProxyTypeProxyObject:
	      case ProxyTypeES5Object:
	      case ProxyTypeMap:
	        return generatePatchesFromAssigned(state, basePath, patches, inversePatches);

	      case ProxyTypeES5Array:
	      case ProxyTypeProxyArray:
	        return generateArrayPatches(state, basePath, patches, inversePatches);

	      case ProxyTypeSet:
	        return generateSetPatches(state, basePath, patches, inversePatches);
	    }
	  }

	  function generateArrayPatches(state, basePath, patches, inversePatches) {
	    var base_ = state.base_,
	        assigned_ = state.assigned_;
	    var copy_ = state.copy_; // Reduce complexity by ensuring `base` is never longer.

	    if (copy_.length < base_.length) {
	      var _ref = [copy_, base_];
	      base_ = _ref[0];
	      copy_ = _ref[1];
	      var _ref2 = [inversePatches, patches];
	      patches = _ref2[0];
	      inversePatches = _ref2[1];
	    } // Process replaced indices.


	    for (var i = 0; i < base_.length; i++) {
	      if (assigned_[i] && copy_[i] !== base_[i]) {
	        var path = basePath.concat([i]);
	        patches.push({
	          op: REPLACE,
	          path: path,
	          // Need to maybe clone it, as it can in fact be the original value
	          // due to the base/copy inversion at the start of this function
	          value: clonePatchValueIfNeeded(copy_[i])
	        });
	        inversePatches.push({
	          op: REPLACE,
	          path: path,
	          value: clonePatchValueIfNeeded(base_[i])
	        });
	      }
	    } // Process added indices.


	    for (var _i = base_.length; _i < copy_.length; _i++) {
	      var _path = basePath.concat([_i]);

	      patches.push({
	        op: ADD,
	        path: _path,
	        // Need to maybe clone it, as it can in fact be the original value
	        // due to the base/copy inversion at the start of this function
	        value: clonePatchValueIfNeeded(copy_[_i])
	      });
	    }

	    if (base_.length < copy_.length) {
	      inversePatches.push({
	        op: REPLACE,
	        path: basePath.concat(["length"]),
	        value: base_.length
	      });
	    }
	  } // This is used for both Map objects and normal objects.


	  function generatePatchesFromAssigned(state, basePath, patches, inversePatches) {
	    var base_ = state.base_,
	        copy_ = state.copy_;
	    each(state.assigned_, function (key, assignedValue) {
	      var origValue = get(base_, key);
	      var value = get(copy_, key);
	      var op = !assignedValue ? REMOVE : has(base_, key) ? REPLACE : ADD;
	      if (origValue === value && op === REPLACE) return;
	      var path = basePath.concat(key);
	      patches.push(op === REMOVE ? {
	        op: op,
	        path: path
	      } : {
	        op: op,
	        path: path,
	        value: value
	      });
	      inversePatches.push(op === ADD ? {
	        op: REMOVE,
	        path: path
	      } : op === REMOVE ? {
	        op: ADD,
	        path: path,
	        value: clonePatchValueIfNeeded(origValue)
	      } : {
	        op: REPLACE,
	        path: path,
	        value: clonePatchValueIfNeeded(origValue)
	      });
	    });
	  }

	  function generateSetPatches(state, basePath, patches, inversePatches) {
	    var base_ = state.base_,
	        copy_ = state.copy_;
	    var i = 0;
	    base_.forEach(function (value) {
	      if (!copy_.has(value)) {
	        var path = basePath.concat([i]);
	        patches.push({
	          op: REMOVE,
	          path: path,
	          value: value
	        });
	        inversePatches.unshift({
	          op: ADD,
	          path: path,
	          value: value
	        });
	      }

	      i++;
	    });
	    i = 0;
	    copy_.forEach(function (value) {
	      if (!base_.has(value)) {
	        var path = basePath.concat([i]);
	        patches.push({
	          op: ADD,
	          path: path,
	          value: value
	        });
	        inversePatches.unshift({
	          op: REMOVE,
	          path: path,
	          value: value
	        });
	      }

	      i++;
	    });
	  }

	  function generateReplacementPatches_(rootState, replacement, patches, inversePatches) {
	    patches.push({
	      op: REPLACE,
	      path: [],
	      value: replacement
	    });
	    inversePatches.push({
	      op: REPLACE,
	      path: [],
	      value: rootState.base_
	    });
	  }

	  function applyPatches_(draft, patches) {
	    patches.forEach(function (patch) {
	      var path = patch.path,
	          op = patch.op;
	      var base = draft;

	      for (var i = 0; i < path.length - 1; i++) {
	        base = get(base, path[i]);
	        if (typeof base !== "object") die(15, path.join("/"));
	      }

	      var type = getArchtype(base);
	      var value = deepClonePatchValue(patch.value); // used to clone patch to ensure original patch is not modified, see #411

	      var key = path[path.length - 1];

	      switch (op) {
	        case REPLACE:
	          switch (type) {
	            case ArchtypeMap:
	              return base.set(key, value);

	            /* istanbul ignore next */

	            case ArchtypeSet:
	              die(16);

	            default:
	              // if value is an object, then it's assigned by reference
	              // in the following add or remove ops, the value field inside the patch will also be modifyed
	              // so we use value from the cloned patch
	              // @ts-ignore
	              return base[key] = value;
	          }

	        case ADD:
	          switch (type) {
	            case ArchtypeArray:
	              return base.splice(key, 0, value);

	            case ArchtypeMap:
	              return base.set(key, value);

	            case ArchtypeSet:
	              return base.add(value);

	            default:
	              return base[key] = value;
	          }

	        case REMOVE:
	          switch (type) {
	            case ArchtypeArray:
	              return base.splice(key, 1);

	            case ArchtypeMap:
	              return base.delete(key);

	            case ArchtypeSet:
	              return base.delete(patch.value);

	            default:
	              return delete base[key];
	          }

	        default:
	          die(17, op);
	      }
	    });
	    return draft;
	  }

	  function deepClonePatchValue(obj) {
	    if (!obj || typeof obj !== "object") return obj;
	    if (Array.isArray(obj)) return obj.map(deepClonePatchValue);
	    if (isMap(obj)) return new Map(Array.from(obj.entries()).map(function (_ref3) {
	      var k = _ref3[0],
	          v = _ref3[1];
	      return [k, deepClonePatchValue(v)];
	    }));
	    if (isSet(obj)) return new Set(Array.from(obj).map(deepClonePatchValue));
	    var cloned = Object.create(Object.getPrototypeOf(obj));

	    for (var key in obj) {
	      cloned[key] = deepClonePatchValue(obj[key]);
	    }

	    return cloned;
	  }

	  function clonePatchValueIfNeeded(obj) {
	    if (isDraft(obj)) {
	      return deepClonePatchValue(obj);
	    } else return obj;
	  }

	  loadPlugin("Patches", {
	    applyPatches_: applyPatches_,
	    generatePatches_: generatePatches_,
	    generateReplacementPatches_: generateReplacementPatches_
	  });
	}

	// types only!
	function enableMapSet() {
	  /* istanbul ignore next */
	  var _extendStatics = function extendStatics(d, b) {
	    _extendStatics = Object.setPrototypeOf || {
	      __proto__: []
	    } instanceof Array && function (d, b) {
	      d.__proto__ = b;
	    } || function (d, b) {
	      for (var p in b) {
	        if (b.hasOwnProperty(p)) d[p] = b[p];
	      }
	    };

	    return _extendStatics(d, b);
	  }; // Ugly hack to resolve #502 and inherit built in Map / Set


	  function __extends(d, b) {
	    _extendStatics(d, b);

	    function __() {
	      this.constructor = d;
	    }

	    d.prototype = ( // @ts-ignore
	    __.prototype = b.prototype, new __());
	  }

	  var DraftMap = function (_super) {
	    __extends(DraftMap, _super); // Create class manually, cause #502


	    function DraftMap(target, parent) {
	      this[DRAFT_STATE] = {
	        type_: ProxyTypeMap,
	        parent_: parent,
	        scope_: parent ? parent.scope_ : getCurrentScope(),
	        modified_: false,
	        finalized_: false,
	        copy_: undefined,
	        assigned_: undefined,
	        base_: target,
	        draft_: this,
	        isManual_: false,
	        revoked_: false
	      };
	      return this;
	    }

	    var p = DraftMap.prototype;
	    Object.defineProperty(p, "size", {
	      get: function get() {
	        return latest(this[DRAFT_STATE]).size;
	      } // enumerable: false,
	      // configurable: true

	    });

	    p.has = function (key) {
	      return latest(this[DRAFT_STATE]).has(key);
	    };

	    p.set = function (key, value) {
	      var state = this[DRAFT_STATE];
	      assertUnrevoked(state);

	      if (!latest(state).has(key) || latest(state).get(key) !== value) {
	        prepareMapCopy(state);
	        markChanged(state);
	        state.assigned_.set(key, true);
	        state.copy_.set(key, value);
	        state.assigned_.set(key, true);
	      }

	      return this;
	    };

	    p.delete = function (key) {
	      if (!this.has(key)) {
	        return false;
	      }

	      var state = this[DRAFT_STATE];
	      assertUnrevoked(state);
	      prepareMapCopy(state);
	      markChanged(state);
	      state.assigned_.set(key, false);
	      state.copy_.delete(key);
	      return true;
	    };

	    p.clear = function () {
	      var state = this[DRAFT_STATE];
	      assertUnrevoked(state);
	      prepareMapCopy(state);
	      markChanged(state);
	      state.assigned_ = new Map();
	      each(state.base_, function (key) {
	        state.assigned_.set(key, false);
	      });
	      return state.copy_.clear();
	    };

	    p.forEach = function (cb, thisArg) {
	      var _this = this;

	      var state = this[DRAFT_STATE];
	      latest(state).forEach(function (_value, key, _map) {
	        cb.call(thisArg, _this.get(key), key, _this);
	      });
	    };

	    p.get = function (key) {
	      var state = this[DRAFT_STATE];
	      assertUnrevoked(state);
	      var value = latest(state).get(key);

	      if (state.finalized_ || !isDraftable(value)) {
	        return value;
	      }

	      if (value !== state.base_.get(key)) {
	        return value; // either already drafted or reassigned
	      } // despite what it looks, this creates a draft only once, see above condition


	      var draft = createProxy(state.scope_.immer_, value, state);
	      prepareMapCopy(state);
	      state.copy_.set(key, draft);
	      return draft;
	    };

	    p.keys = function () {
	      return latest(this[DRAFT_STATE]).keys();
	    };

	    p.values = function () {
	      var _this2 = this,
	          _ref;

	      var iterator = this.keys();
	      return _ref = {}, _ref[iteratorSymbol] = function () {
	        return _this2.values();
	      }, _ref.next = function next() {
	        var r = iterator.next();
	        /* istanbul ignore next */

	        if (r.done) return r;

	        var value = _this2.get(r.value);

	        return {
	          done: false,
	          value: value
	        };
	      }, _ref;
	    };

	    p.entries = function () {
	      var _this3 = this,
	          _ref2;

	      var iterator = this.keys();
	      return _ref2 = {}, _ref2[iteratorSymbol] = function () {
	        return _this3.entries();
	      }, _ref2.next = function next() {
	        var r = iterator.next();
	        /* istanbul ignore next */

	        if (r.done) return r;

	        var value = _this3.get(r.value);

	        return {
	          done: false,
	          value: [r.value, value]
	        };
	      }, _ref2;
	    };

	    p[iteratorSymbol] = function () {
	      return this.entries();
	    };

	    return DraftMap;
	  }(Map);

	  function proxyMap_(target, parent) {
	    // @ts-ignore
	    return new DraftMap(target, parent);
	  }

	  function prepareMapCopy(state) {
	    if (!state.copy_) {
	      state.assigned_ = new Map();
	      state.copy_ = new Map(state.base_);
	    }
	  }

	  var DraftSet = function (_super) {
	    __extends(DraftSet, _super); // Create class manually, cause #502


	    function DraftSet(target, parent) {
	      this[DRAFT_STATE] = {
	        type_: ProxyTypeSet,
	        parent_: parent,
	        scope_: parent ? parent.scope_ : getCurrentScope(),
	        modified_: false,
	        finalized_: false,
	        copy_: undefined,
	        base_: target,
	        draft_: this,
	        drafts_: new Map(),
	        revoked_: false,
	        isManual_: false
	      };
	      return this;
	    }

	    var p = DraftSet.prototype;
	    Object.defineProperty(p, "size", {
	      get: function get() {
	        return latest(this[DRAFT_STATE]).size;
	      } // enumerable: true,

	    });

	    p.has = function (value) {
	      var state = this[DRAFT_STATE];
	      assertUnrevoked(state); // bit of trickery here, to be able to recognize both the value, and the draft of its value

	      if (!state.copy_) {
	        return state.base_.has(value);
	      }

	      if (state.copy_.has(value)) return true;
	      if (state.drafts_.has(value) && state.copy_.has(state.drafts_.get(value))) return true;
	      return false;
	    };

	    p.add = function (value) {
	      var state = this[DRAFT_STATE];
	      assertUnrevoked(state);

	      if (!this.has(value)) {
	        prepareSetCopy(state);
	        markChanged(state);
	        state.copy_.add(value);
	      }

	      return this;
	    };

	    p.delete = function (value) {
	      if (!this.has(value)) {
	        return false;
	      }

	      var state = this[DRAFT_STATE];
	      assertUnrevoked(state);
	      prepareSetCopy(state);
	      markChanged(state);
	      return state.copy_.delete(value) || (state.drafts_.has(value) ? state.copy_.delete(state.drafts_.get(value)) :
	      /* istanbul ignore next */
	      false);
	    };

	    p.clear = function () {
	      var state = this[DRAFT_STATE];
	      assertUnrevoked(state);
	      prepareSetCopy(state);
	      markChanged(state);
	      return state.copy_.clear();
	    };

	    p.values = function () {
	      var state = this[DRAFT_STATE];
	      assertUnrevoked(state);
	      prepareSetCopy(state);
	      return state.copy_.values();
	    };

	    p.entries = function entries() {
	      var state = this[DRAFT_STATE];
	      assertUnrevoked(state);
	      prepareSetCopy(state);
	      return state.copy_.entries();
	    };

	    p.keys = function () {
	      return this.values();
	    };

	    p[iteratorSymbol] = function () {
	      return this.values();
	    };

	    p.forEach = function forEach(cb, thisArg) {
	      var iterator = this.values();
	      var result = iterator.next();

	      while (!result.done) {
	        cb.call(thisArg, result.value, result.value, this);
	        result = iterator.next();
	      }
	    };

	    return DraftSet;
	  }(Set);

	  function proxySet_(target, parent) {
	    // @ts-ignore
	    return new DraftSet(target, parent);
	  }

	  function prepareSetCopy(state) {
	    if (!state.copy_) {
	      // create drafts for all entries to preserve insertion order
	      state.copy_ = new Set();
	      state.base_.forEach(function (value) {
	        if (isDraftable(value)) {
	          var draft = createProxy(state.scope_.immer_, value, state);
	          state.drafts_.set(value, draft);
	          state.copy_.add(draft);
	        } else {
	          state.copy_.add(value);
	        }
	      });
	    }
	  }

	  function assertUnrevoked(state
	  /*ES5State | MapState | SetState*/
	  ) {
	    if (state.revoked_) die(3, JSON.stringify(latest(state)));
	  }

	  loadPlugin("MapSet", {
	    proxyMap_: proxyMap_,
	    proxySet_: proxySet_
	  });
	}

	function enableAllPlugins() {
	  enableES5();
	  enableMapSet();
	  enablePatches();
	}

	var immer =
	/*#__PURE__*/
	new Immer();
	/**
	 * The `produce` function takes a value and a "recipe function" (whose
	 * return value often depends on the base state). The recipe function is
	 * free to mutate its first argument however it wants. All mutations are
	 * only ever applied to a __copy__ of the base state.
	 *
	 * Pass only a function to create a "curried producer" which relieves you
	 * from passing the recipe function every time.
	 *
	 * Only plain objects and arrays are made mutable. All other objects are
	 * considered uncopyable.
	 *
	 * Note: This function is __bound__ to its `Immer` instance.
	 *
	 * @param {any} base - the initial state
	 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified
	 * @param {Function} patchListener - optional function that will be called with all the patches produced here
	 * @returns {any} a new state, or the initial state if nothing was modified
	 */

	var produce = immer.produce;
	/**
	 * Like `produce`, but `produceWithPatches` always returns a tuple
	 * [nextState, patches, inversePatches] (instead of just the next state)
	 */

	var produceWithPatches =
	/*#__PURE__*/
	immer.produceWithPatches.bind(immer);
	/**
	 * Pass true to automatically freeze all copies created by Immer.
	 *
	 * By default, auto-freezing is disabled in production.
	 */

	var setAutoFreeze =
	/*#__PURE__*/
	immer.setAutoFreeze.bind(immer);
	/**
	 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is
	 * always faster than using ES5 proxies.
	 *
	 * By default, feature detection is used, so calling this is rarely necessary.
	 */

	var setUseProxies =
	/*#__PURE__*/
	immer.setUseProxies.bind(immer);
	/**
	 * Apply an array of Immer patches to the first argument.
	 *
	 * This function is a producer, which means copy-on-write is in effect.
	 */

	var applyPatches =
	/*#__PURE__*/
	immer.applyPatches.bind(immer);
	/**
	 * Create an Immer draft from the given base state, which may be a draft itself.
	 * The draft can be modified until you finalize it with the `finishDraft` function.
	 */

	var createDraft =
	/*#__PURE__*/
	immer.createDraft.bind(immer);
	/**
	 * Finalize an Immer draft from a `createDraft` call, returning the base state
	 * (if no changes were made) or a modified copy. The draft must *not* be
	 * mutated afterwards.
	 *
	 * Pass a function as the 2nd argument to generate Immer patches based on the
	 * changes that were made.
	 */

	var finishDraft =
	/*#__PURE__*/
	immer.finishDraft.bind(immer);
	/**
	 * This function is actually a no-op, but can be used to cast an immutable type
	 * to an draft type and make TypeScript happy
	 *
	 * @param value
	 */

	function castDraft(value) {
	  return value;
	}
	/**
	 * This function is actually a no-op, but can be used to cast a mutable type
	 * to an immutable type and make TypeScript happy
	 * @param value
	 */

	function castImmutable(value) {
	  return value;
	}

	exports.Immer = Immer;
	exports.applyPatches = applyPatches;
	exports.castDraft = castDraft;
	exports.castImmutable = castImmutable;
	exports.createDraft = createDraft;
	exports.current = current;
	exports.default = produce;
	exports.enableAllPlugins = enableAllPlugins;
	exports.enableES5 = enableES5;
	exports.enableMapSet = enableMapSet;
	exports.enablePatches = enablePatches;
	exports.finishDraft = finishDraft;
	exports.immerable = DRAFTABLE;
	exports.isDraft = isDraft;
	exports.isDraftable = isDraftable;
	exports.nothing = NOTHING;
	exports.original = original;
	exports.produce = produce;
	exports.produceWithPatches = produceWithPatches;
	exports.setAutoFreeze = setAutoFreeze;
	exports.setUseProxies = setUseProxies;

	Object.defineProperty(exports, '__esModule', { value: true });

})));
//# sourceMappingURL=immer.umd.development.js.map