All files / ext.wikilambda.edit/store/modules/zobject addZObjects.js

100% Statements 159/159
92.3% Branches 84/91
100% Functions 42/42
100% Lines 157/157

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            21x 21x 21x 21x     21x                                                         158x   9x   149x         149x 5x 1x 1x 1x 1x       148x   19x   25x   1x   4x   16x   8x   8x   13x   7x   10x   2x   4x   2x   1x   5x   3x   3x   17x     195x                                                 17x 17x     17x 17x 11x 11x 11x 11x 15x     15x                 15x           15x 15x       17x   63x                                                                     10x   10x 10x 10x 9x 9x 9x   9x     10x   69x                                                     16x   16x 16x 16x 16x   73x                                                     8x   8x 8x 8x 7x 8x     8x   65x                                                         4x   4x 2x 2x 2x 2x 2x   4x   61x                                           25x   79x                                         28x   28x 28x   77x                                               1x   1x 1x   61x                                                 2x   2x 2x   61x                                                     8x   8x 8x   66x                                         13x   13x 13x   70x                                               4x   4x 4x 4x   63x                                                 2x   62x                                                       7x 7x   7x 7x 7x 7x 7x   66x                                                             1x     1x 1x 1x   62x                                         5x   5x 5x   61x                                                             3x   3x 3x 3x 3x 3x 3x 3x 3x 3x   61x                                                       3x     3x 3x 3x 3x 3x   61x                                   113x 113x     113x       113x 113x     113x                               1x   3x 1x 2x 2x          
/*!
 * WikiLambda Vuex code to interact with the store to create and update ZObjects.
 *
 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
 * @license MIT
 */
const Constants = require( '../../../Constants.js' ),
	typeUtils = require( '../../../mixins/typeUtils.js' ).methods,
	extractZIDs = require( '../../../mixins/schemata.js' ).methods.extractZIDs,
	url = require( '../../../mixins/urlUtils.js' ).methods;
 
/* eslint-disable no-unused-vars */
module.exports = exports = {
	getters: {
		/**
		 * Return a blank object for a given type, and initialize its
		 * values if the payload contains the required initialization data.
		 *
		 * @param {Object} _state
		 * @param {Object} getters
		 * @return {Function}
		 */
		createObjectByType: function ( _state, getters ) {
			/**
			 * @param {Object} payload
			 * @param {string} payload.type the type of the new object to add
			 * @param {number} payload.id the parent rowId for the new object
			 * @param {Object} payload.value initialization values
			 * @param {boolean} payload.append whether to append the new zobject to a list
			 * @param {boolean} payload.link whether to default to reference over literal
			 * @param {Array} keyList a list of types that have been seen so far
			 * @return {Object}
			 */
			function newObjectByType( payload, keyList = [] ) {
				// If payload.link is true, we are prioritizing creating a
				// blank reference over the literal object. This happens for the
				// types Constants.LINKED_TYPES, which are generally persisted and
				// linked (Z8/Function, Z4/Type, etc.)
				// We want to do this when we add new keys, new items to a list,
				// arguments to a function call, or similar cases. We don't want
				// to do this when we create the blank object for the page root.
				if ( keyList.includes( payload.type ) ||
						( payload.link && Constants.LINKED_TYPES.includes( payload.type ) ) ) {
					return getters.createZReference( payload );
				}
				keyList.push( payload.type );
 
				// If payload.type is an object, we are looking at a generic type,
				// so a type returned by a function call. Transform the payload for
				// the spacail cases: List, Pair and Map
				if ( typeUtils.isGenericType( payload.type ) ) {
					if ( payload.type[ Constants.Z_FUNCTION_CALL_FUNCTION ] === Constants.Z_TYPED_LIST ) {
						const newPayload = JSON.parse( JSON.stringify( payload ) );
						newPayload.type = Constants.Z_TYPED_LIST;
						newPayload.value = payload.type[ Constants.Z_TYPED_LIST_TYPE ];
						return getters.createObjectByType( newPayload, keyList );
					}
				}
 
				switch ( payload.type ) {
					case Constants.Z_REFERENCE:
						return getters.createZReference( payload );
					case Constants.Z_STRING:
						return getters.createZString( payload );
					case Constants.Z_BOOLEAN:
						return getters.createZBoolean( payload );
					case Constants.Z_MULTILINGUALSTRING:
						return getters.createZMultilingualString( payload );
					case Constants.Z_MONOLINGUALSTRING:
						return getters.createZMonolingualString( payload );
					case Constants.Z_MONOLINGUALSTRINGSET:
						return getters.createZMonolingualStringSet( payload );
					case Constants.Z_ARGUMENT:
						return getters.createZArgument( payload );
					case Constants.Z_FUNCTION_CALL:
						return getters.createZFunctionCall( payload );
					case Constants.Z_FUNCTION:
						return getters.createZFunction( payload );
					case Constants.Z_PERSISTENTOBJECT:
						return getters.createZPersistentObject( payload );
					case Constants.Z_TYPE:
						return getters.createZType( payload );
					case Constants.Z_IMPLEMENTATION:
						return getters.createZImplementation( payload );
					case Constants.Z_CODE:
						return getters.createZCode( payload );
					case Constants.Z_TESTER:
						return getters.createZTester( payload );
					case Constants.Z_TYPED_LIST:
						return getters.createZTypedList( payload );
					case Constants.Z_TYPED_PAIR:
						return getters.createZTypedPair( payload );
					case Constants.Z_TYPED_MAP:
						return getters.createZTypedMap( payload );
					default:
						return getters.createGenericObject( payload, keyList );
				}
			}
			return newObjectByType;
		},
 
		/**
		 * Return a blank generic object for a given type. If the
		 * type is a reference and the persisted object is known,
		 * initialize all its keys. Else simply return the type field.
		 * The entry will result in a json representation equal to:
		 * {
		 *  Z1K1: 'Zx'
		 *  ZxK1: ''
		 * }
		 *
		 * @param {Object} _state
		 * @param {Object} getters
		 * @return {Function}
		 */
		createGenericObject: function ( _state, getters ) {
			/**
			 * @param {Object} payload
			 * @param {string} payload.type
			 * @param {Array} keyList a list of types that have been seen so far
			 * @return {Object}
			 */
			function newGenericObject( payload, keyList = [] ) {
				const persisted = getters.getStoredObject( payload.type );
				const value = {
					[ Constants.Z_OBJECT_TYPE ]: payload.type
				};
				keyList.push( payload.type );
				if ( persisted ) {
					const zobject = persisted[ Constants.Z_PERSISTENTOBJECT_VALUE ];
					Eif ( zobject[ Constants.Z_OBJECT_TYPE ] === Constants.Z_TYPE ) {
						const keys = zobject[ Constants.Z_TYPE_KEYS ];
						for ( let i = 1; i < keys.length; i++ ) {
							const key = keys[ i ];
 
							// Create a reference to self it the key is the identity key of the root object
							const isIdentityKey = !payload.isRoot ? false : (
								typeUtils.isTruthyOrEqual( key, [
									Constants.Z_KEY_IS_IDENTITY
								], Constants.Z_BOOLEAN_TRUE ) ||
								typeUtils.isTruthyOrEqual( key, [
									Constants.Z_KEY_IS_IDENTITY,
									Constants.Z_BOOLEAN_IDENTITY
								], Constants.Z_BOOLEAN_TRUE )
							);
							const keyPayload = isIdentityKey ?
								{ type: Constants.Z_REFERENCE, value: getters.getCurrentZObjectId } :
								typeUtils.initializePayloadForType( key[ Constants.Z_KEY_TYPE ] );
 
							// We must pass keyList array by value in here, so that the types found in an argument
							// branch don't affect another branch, it should only restrict repetition in depth.
							const blankValue = getters.createObjectByType( keyPayload, keyList.slice() );
							value[ key[ Constants.Z_KEY_ID ] ] = blankValue;
						}
					}
				}
				return value;
			}
			return newGenericObject;
		},
 
		/**
		 * Return a blank and initialized zPersistentObject.
		 * The value will result in a json representation equal to:
		 * {
		 *  Z1K1: { Z1K1: 'Z9', Z9K1: 'Z2' },
		 *  Z2K1: { Z1K1: 'Z9', Z9K1: 'Z0' },
		 *  Z2K2: {
		 *   Z1K1: { Z1K1: 'Z9', Z9K1: '' }
		 *  },
		 *  Z2K3: {
		 *   Z1K1: { Z1K1: 'Z9', Z9K1: 'Z12' }
		 *   Z12K1: [ Z11 ]
		 *  },
		 *  Z2K4: {
		 *   Z1K1: { Z1K1: 'Z9', Z9K1: 'Z32' }
		 *   Z32K1: [ Z31 ]
		 *  }
		 * }
		 *
		 * @param {Object} _state
		 * @param {Object} getters
		 * @return {Function}
		 */
		createZPersistentObject: function ( _state, getters ) {
			/**
			 * @param {Object} _payload
			 * @param {number} _payload.id
			 * @param {boolean} _payload.append
			 * @return {Object}
			 */
			function newZPersistentObject( _payload ) {
				// Get scaffolding
				const value = typeUtils.getScaffolding( Constants.Z_PERSISTENTOBJECT );
				// Initialize persistent zid and blank label
				const zid = getters.getCurrentZObjectId || Constants.NEW_ZID_PLACEHOLDER;
				value[ Constants.Z_PERSISTENTOBJECT_ID ][ Constants.Z_STRING_VALUE ] = zid;
				if ( getters.getUserLangZid ) {
					const mono = typeUtils.getScaffolding( Constants.Z_MONOLINGUALSTRING );
					mono[ Constants.Z_MONOLINGUALSTRING_VALUE ] = '';
					mono[ Constants.Z_MONOLINGUALSTRING_LANGUAGE ][
						Constants.Z_REFERENCE_ID ] = getters.getUserLangZid;
					value[ Constants.Z_PERSISTENTOBJECT_LABEL ][
						Constants.Z_MULTILINGUALSTRING_VALUE ].push( mono );
				}
				return value;
			}
			return newZPersistentObject;
		},
 
		/**
		 * Return a blank and initialized zMonolingualString.
		 * The value will result in a json representation equal to:
		 * {
		 *  Z1K1: Z11,
		 *  Z11K1: { Z1K1: Z9, Z9K1: payload.lang },
		 *  Z11K2: { Z1K1: Z6, Z6K1: '' }
		 * }
		 *
		 * @param {Object} _state
		 * @param {Object} getters
		 * @return {Function}
		 */
		createZMonolingualString: function ( _state, getters ) {
			/**
			 * @param {Object} payload
			 * @param {number} payload.id
			 * @param {string} payload.value string value of the monolingual string
			 * @param {string} payload.lang zid of the language for the monolingual string
			 * @param {boolean} payload.append
			 * @return {Object}
			 */
			function newZMonolingualString( payload ) {
				// Get scaffolding
				const value = typeUtils.getScaffolding( Constants.Z_MONOLINGUALSTRING );
				// Initialize monolingual string
				const lang = payload.lang || '';
				value[ Constants.Z_MONOLINGUALSTRING_VALUE ] = payload.value || '';
				value[ Constants.Z_MONOLINGUALSTRING_LANGUAGE ][ Constants.Z_REFERENCE_ID ] = lang;
				return value;
			}
			return newZMonolingualString;
		},
 
		/**
		 * Return a blank and initialized zMonolingualStringSet.
		 * The value will result in a json representation equal to:
		 * {
		 *  Z1K1: Z31,
		 *  Z31K1: { Z1K1: Z9, Z9K1: payload.lang },
		 *  Z31K2: [ 'Z6', { Z1K1: Z6, Z6K1: payload.value } ]
		 * }
		 *
		 * @param {Object} _state
		 * @param {Object} getters
		 * @return {Function}
		 */
		createZMonolingualStringSet: function ( _state, getters ) {
			/**
			 * @param {Object} payload
			 * @param {number} payload.id
			 * @param {Array} payload.value Array list of strings for the list
			 * @param {string} payload.lang zid of the language for the first monolingual string
			 * @param {boolean} payload.append
			 * @return {Object}
			 */
			function newZMonolingualStringSet( payload ) {
				// Get scaffolding
				const value = typeUtils.getScaffolding( Constants.Z_MONOLINGUALSTRINGSET );
				// Initialize language and first string
				const lang = payload.lang || '';
				value[ Constants.Z_MONOLINGUALSTRINGSET_LANGUAGE ][ Constants.Z_REFERENCE_ID ] = lang;
				if ( payload.value ) {
					payload.value.forEach( ( stringValue ) => {
						value[ Constants.Z_MONOLINGUALSTRINGSET_VALUE ].push( stringValue );
					} );
				}
				return value;
			}
			return newZMonolingualStringSet;
		},
 
		/**
		 * Return a blank and initialized zMulilingualString.
		 * The value will result in a json representation equal to:
		 * {
		 *  Z1K1: Z12,
		 *  Z12K1: [
		 *   Z11,
		 *   { Z1K1: Z11, Z11K1: '', Z11K2: '' }
		 *  ]
		 * }
		 *
		 * @param {Object} _state
		 * @param {Object} getters
		 * @return {Function}
		 */
		createZMultilingualString: function ( _state, getters ) {
			/**
			 * @param {Object} payload
			 * @param {number} payload.id
			 * @param {string} payload.value string value of the first monolingual string
			 * @param {string} payload.lang zid of the language for the first monolingual string
			 * @param {number} payload.append
			 * @return {Object}
			 */
			function newZMultilingualString( payload ) {
				// Get scaffolding
				const value = typeUtils.getScaffolding( Constants.Z_MULTILINGUALSTRING );
				// Initialize first monolingual string if there's any lang or value
				if ( ( 'lang' in payload ) || ( 'value' in payload ) ) {
					const mono = typeUtils.getScaffolding( Constants.Z_MONOLINGUALSTRING );
					const lang = payload.lang || getters.getUserLangZid;
					mono[ Constants.Z_MONOLINGUALSTRING_VALUE ] = payload.value || '';
					mono[ Constants.Z_MONOLINGUALSTRING_LANGUAGE ][ Constants.Z_REFERENCE_ID ] = lang;
					value[ Constants.Z_MULTILINGUALSTRING_VALUE ].push( mono );
				}
				return value;
			}
			return newZMultilingualString;
		},
 
		/**
		 * Return a blank and initialized zString
		 * The value will result in a json representation equal to:
		 * { Z1K1: Z6, Z6K1: payload.value }
		 *
		 * @param {Object} _state
		 * @return {Function}
		 */
		createZString: function ( _state ) {
			/**
			 * @param {Object} payload
			 * @param {number} payload.id
			 * @param {string} payload.value
			 * @param {boolean} payload.append
			 * @return {Object}
			 */
			function newZString( payload ) {
				// No need to get scaffolding, the value is a canonical string, so
				// either it's a blank string or a string with a value.
				return payload.value || '';
			}
			return newZString;
		},
 
		/**
		 * Return a blank and initialized zReference.
		 * The value will result in a json representation equal to:
		 * { Z1K1: Z9, Z9K1: payload.value }
		 *
		 * @param {Object} _state
		 * @return {Function}
		 */
		createZReference: function ( _state ) {
			/**
			 * @param {Object} payload
			 * @param {number} payload.id
			 * @param {string} payload.value
			 * @param {boolean} payload.append
			 * @return {Object}
			 */
			function newZReference( payload ) {
				// Get scaffolding
				const value = typeUtils.getScaffolding( Constants.Z_REFERENCE );
				// Initialize values, if any
				value[ Constants.Z_REFERENCE_ID ] = payload.value || '';
				return value;
			}
			return newZReference;
		},
 
		/**
		 * Return a blank and initialized zBoolean.
		 * The value will result in a json representation equal to:
		 * {
		 *   Z1K1: Z40,
		 *   Z40K1: { Z1K1: Z9, Z9K1: payload.value }
		 * }
		 *
		 * @param {Object} _state
		 * @return {Function}
		 */
		createZBoolean: function ( _state ) {
			/**
			 * @param {Object} payload
			 * @param {number} payload.id
			 * @param {string} payload.value
			 * @param {boolean} payload.append
			 * @return {Object}
			 */
			function newZBoolean( payload ) {
				// Get scaffolding
				const value = typeUtils.getScaffolding( Constants.Z_BOOLEAN );
				// Initialize value with the boolean Zid, if any
				value[ Constants.Z_BOOLEAN_IDENTITY ][ Constants.Z_REFERENCE_ID ] = payload.value || '';
				return value;
			}
			return newZBoolean;
		},
 
		/**
		 * Return a blank and initialized zType.
		 * The value will result in a json representation equal to:
		 * {
		 *  Z1K1: 'Z4',
		 *  Z4K1: { Z1K1: 'Z9', Z9K1: 'Z0' },
		 *  Z4K2: [ 'Z3' ]
		 *  Z4K3: { Z1K1: 'Z9', Z9K1: 'Z101' }
		 * }
		 *
		 * @param {Object} _state
		 * @return {Function}
		 */
		createZType: function ( _state ) {
			/**
			 * @param {Object} _payload
			 * @param {number} _payload.id
			 * @param {number} _payload.append
			 * @return {Object}
			 */
			function newZType( _payload ) {
				// Get scaffolding
				const value = typeUtils.getScaffolding( Constants.Z_TYPE );
				// Initialize validator function
				value[ Constants.Z_TYPE_VALIDATOR ][ Constants.Z_REFERENCE_ID ] = Constants.Z_VALIDATE_OBJECT;
				return value;
			}
			return newZType;
		},
 
		/**
		 * Return a blank and initialized zArgument.
		 * The value will result in a json representation equal to:
		 * {
		 *  Z1K1: 'Z17',
		 *  Z17K1: { Z1K1: 'Z9', Z9K1: '' },
		 *  Z17K2: { Z1K1: 'Z6', Z6K1: 'Z0K1' },
		 *  Z17K3: { Z1K1: 'Z12', Z12K1: [ Z11 ] }
		 * }
		 *
		 * @param {Object} _state
		 * @param {Object} getters
		 * @return {Function}
		 */
		createZArgument: function ( _state, getters ) {
			/**
			 * @param {Object} payload
			 * @param {number} payload.id
			 * @param {string} payload.value
			 * @param {boolean} payload.append
			 * @return {Object}
			 */
			function newZArgument( payload ) {
				// Get scaffolding
				const value = typeUtils.getScaffolding( Constants.Z_ARGUMENT );
				// Initialize argument key
				value[ Constants.Z_ARGUMENT_KEY ] = payload.value || getters.getNextKey;
				return value;
			}
			return newZArgument;
		},
 
		/**
		 * Return a blank and initialized zFunctionCall.
		 * The value will result in a json representation equal to:
		 * { Z1K1: Z7, Z7K1: '' }
		 *
		 * @param {Object} _state
		 * @return {Function}
		 */
		createZFunctionCall: function ( _state ) {
			/**
			 * @param {Object} payload
			 * @param {number} payload.id
			 * @param {string} payload.value Zid of the function to call
			 * @param {boolean} payload.append
			 * @return {Object}
			 */
			function newZFunctionCall( payload ) {
				// Get scaffolding
				const value = typeUtils.getScaffolding( Constants.Z_FUNCTION_CALL );
				// Initialize function zid
				value[ Constants.Z_FUNCTION_CALL_FUNCTION ][ Constants.Z_REFERENCE_ID ] = payload.value || '';
				return value;
			}
			return newZFunctionCall;
		},
 
		/**
		 * Return a blank and initialized zImplementation.
		 * The value will result in a json representation equal to:
		 * {
		 *  Z1K1: 'Z14',
		 *  Z14K1: { Z1K1: 'Z9', Z9K1: '' },
		 *  Z14K2: { Z1K1: 'Z7', Z7K1: '' },
		 * }
		 *
		 * @param {Object} _state
		 * @return {Function}
		 */
		createZImplementation: function ( _state ) {
			/**
			 * @param {Object} _payload
			 * @param {number} _payload.id
			 * @param {boolean} _payload.append
			 * @return {Object}
			 */
			function newZImplementation( _payload ) {
				// Get scaffolding
				const value = typeUtils.getScaffolding( Constants.Z_IMPLEMENTATION );
				// Initialize function zid from the url parameters
				const functionZid = url.getParameterByName( Constants.Z_IMPLEMENTATION_FUNCTION ) || '';
				value[ Constants.Z_IMPLEMENTATION_FUNCTION ][ Constants.Z_REFERENCE_ID ] = functionZid;
				return value;
			}
			return newZImplementation;
		},
 
		/**
		 * Return a blank and initialized zCode.
		 * The value will result in a json representation equal to:
		 *
		 * {
		 *  Z1K1: Z16
		 *  Z16K1: { Z1K1: Z61, Z61K1: '' },
		 *  Z16K2: ''
		 * }
		 *
		 * @param {Object} _state
		 * @return {Function}
		 */
		createZCode: function ( _state ) {
			/**
			 * @param {Object} _payload
			 * @param {number} _payload.id
			 * @param {boolean} _payload.append
			 * @return {Object}
			 */
			function newZCode( _payload ) {
				// Get scaffolding
				return typeUtils.getScaffolding( Constants.Z_CODE );
			}
			return newZCode;
		},
 
		/**
		 * Return a blank and initialized zFunction.
		 * The value will result in a json representation equal to:
		 * {
		 *  Z1K1: 'Z8',
		 *  Z8K1: [ 'Z17' ],
		 *  Z8K2: { Z1K1: 'Z9', Z9K1: '' },
		 *  Z8K3: [ 'Z20' ],
		 *  Z8K4: [ 'Z14' ],
		 *  Z8K5: { Z1K1: 'Z9', Z9K1: '' },
		 * }
		 *
		 * @param {Object} _state
		 * @param {Object} getters
		 * @return {Function}
		 */
		createZFunction: function ( _state, getters ) {
			/**
			 * @param {Object} _payload
			 * @param {number} _payload.id
			 * @param {boolean} _payload.append
			 * @return {Object}
			 */
			function newZFunction( _payload ) {
				// Get scaffolding
				const value = typeUtils.getScaffolding( Constants.Z_FUNCTION );
				const arg = typeUtils.getScaffolding( Constants.Z_ARGUMENT );
				// Initialize function identity and one empty argument
				const functionZid = getters.getCurrentZObjectId || Constants.NEW_ZID_PLACEHOLDER;
				value[ Constants.Z_FUNCTION_IDENTITY ][ Constants.Z_REFERENCE_ID ] = functionZid;
				arg[ Constants.Z_ARGUMENT_KEY ] = `${ functionZid }K1`;
				value[ Constants.Z_FUNCTION_ARGUMENTS ].push( arg );
				return value;
			}
			return newZFunction;
		},
 
		/**
		 * Return a blank and initialized zTester.
		 * The value will result in a json representation equal to:
		 * {
		 *  Z1K1: 'Z20',
		 *  Z20K1: { Z1K1: 'Z9', Z9K1: '' }
		 *  Z20K2: {
		 *   Z1K1: 'Z7',
		 *   Z7K1: { Z1K1: 'Z9', Z9K1: '' }
		 *  },
		 *  Z20K3: {
		 *   Z1K1: 'Z7',
		 *   Z7K1: { Z1K1: 'Z9', Z9K1: '' }
		 *  }
		 * }
		 *
		 * @param {Object} _state
		 * @return {Function}
		 */
		createZTester: function ( _state ) {
			/**
			 * @param {Object} _payload
			 * @param {number} _payload.id
			 * @param {boolean} _payload.append
			 * @return {Object}
			 */
			function newZTester( _payload ) {
				// Get scaffolding
				const value = typeUtils.getScaffolding( Constants.Z_TESTER );
 
				// Initialize function zid from the url parameters
				const functionZid = url.getParameterByName( Constants.Z_TESTER_FUNCTION ) || '';
				value[ Constants.Z_TESTER_FUNCTION ][ Constants.Z_REFERENCE_ID ] = functionZid;
				return value;
			}
			return newZTester;
		},
 
		/**
		 * Return a blank and initialized zTypedList.
		 * The value will result in a json representation equal to:
		 * [ 'Z1' ]
		 *
		 * @param {Object} _state
		 * @return {Function}
		 */
		createZTypedList: function ( _state ) {
			/**
			 * @param {Object} payload
			 * @param {number} payload.id
			 * @param {string} payload.value
			 * @param {boolean} payload.append
			 * @return {Object}
			 */
			function newZTypedList( payload ) {
				// Get scaffolding
				const value = typeUtils.getScaffolding( Constants.Z_TYPED_LIST );
				// Initialize function zid from the url parameters
				value[ 0 ][ Constants.Z_REFERENCE_ID ] = payload.value || Constants.Z_OBJECT;
				return value;
			}
			return newZTypedList;
		},
 
		/**
		 * Return a blank and initialized zTypedPair.
		 * The value will result in a json representation equal to:
		 * {
		 *  Z1K1: {
		 *   Z1K1: Z7,
		 *   Z7K1: Z882,
		 *   Z882K1: { Z1K1: Z9, Z9K1: '' }
		 *   Z882K2: { Z1K1: Z9, Z9K1: '' }
		 *  },
		 *  K1: {}
		 *  K2: {}
		 * }
		 *
		 * @param {Object} _state
		 * @return {Function}
		 */
		createZTypedPair: function ( _state ) {
			/**
			 * @param {Object} payload
			 * @param {number} payload.id
			 * @param {Object} payload.values
			 * @param {boolean} payload.isDeclaration This is used know if the values need to be initialized or not
			 * @param {boolean} payload.append
			 * @return {Object}
			 */
			function newZTypedPair( payload ) {
				// Get scaffolding
				const value = typeUtils.getScaffolding( Constants.Z_TYPED_PAIR );
				// Initialize typed pair types
				const type1 = payload.values ? payload.values[ 0 ] : '';
				const type2 = payload.values ? payload.values[ 1 ] : '';
				const value1 = type1 ? typeUtils.getScaffolding( type1 ) : {};
				const value2 = type2 ? typeUtils.getScaffolding( type2 ) : {};
				value[ Constants.Z_OBJECT_TYPE ][ Constants.Z_TYPED_PAIR_TYPE1 ][ Constants.Z_REFERENCE_ID ] = type1;
				value[ Constants.Z_OBJECT_TYPE ][ Constants.Z_TYPED_PAIR_TYPE2 ][ Constants.Z_REFERENCE_ID ] = type2;
				value[ Constants.Z_TYPED_OBJECT_ELEMENT_1 ] = value1;
				value[ Constants.Z_TYPED_OBJECT_ELEMENT_2 ] = value2;
				return value;
			}
			return newZTypedPair;
		},
 
		/**
		 * Return a blank and initialized zTypedMap.
		 * The value will result in a json representation equal to:
		 * {
		 *  Z1K1: {
		 *   Z1K1: Z7,
		 *   Z7K1: Z883,
		 *   Z883K1: { Z1K1: Z9, Z9K1: '' }
		 *   Z883K2: { Z1K1: Z9, Z9K1: '' }
		 *  }
		 * }
		 *
		 * @param {Object} _state
		 * @return {Function}
		 */
		createZTypedMap: function ( _state ) {
			/**
			 * @param {Object} payload
			 * @param {string} payload.values
			 * @param {number} payload.id
			 * @param {boolean} payload.append
			 * @return {Object}
			 */
			function newZTypedMap( payload ) {
				// Get scaffolding
				const value = typeUtils.getScaffolding( Constants.Z_TYPED_MAP );
 
				// Initialize typed pair types
				const type1 = payload.values ? payload.values[ 0 ] : '';
				const type2 = payload.values ? payload.values[ 1 ] : '';
				value[ Constants.Z_OBJECT_TYPE ][ Constants.Z_TYPED_MAP_TYPE1 ][ Constants.Z_REFERENCE_ID ] = type1;
				value[ Constants.Z_OBJECT_TYPE ][ Constants.Z_TYPED_MAP_TYPE2 ][ Constants.Z_REFERENCE_ID ] = type2;
				return value;
			}
			return newZTypedMap;
		}
 
	},
	actions: {
		/**
		 * Changes the type, inserts or append a specific zObject given its type.
		 *
		 * @param {Object} context
		 * @param {Object} payload
		 * @param {string} payload.type the type of the new object to add
		 * @param {number} payload.id the parent rowId for the new object
		 * @param {boolean} payload.append whether to append the new zobject to a list
		 * @param {Object} payload.value initialization values
		 * @return {Promise}
		 */
		changeType: function ( context, payload ) {
			// Set isRoot flag if we are changing the direct child of Z2K2
			const rootRowId = context.getters.getZPersistentContentRowId();
			payload.isRoot = ( rootRowId === payload.id );
 
			// Build the expected value to assign
			const value = context.getters.createObjectByType( payload );
 
			// Asynchronously fetch the necessary zids. We don't need to wait
			// to the fetch call because these will only be needed for labels.
			const zids = extractZIDs( value );
			context.dispatch( 'fetchZids', { zids } );
 
			// Inject (replace or append) the value from a given row ID
			return context.dispatch( 'injectZObjectFromRowId', {
				rowId: payload.id,
				value,
				append: payload.append || false
			} );
		},
 
		/**
		 * Clears the type, keeping the key Z1K1 and removing all other keys
		 * given a parent rowId
		 *
		 * @param {Object} context
		 * @param {number} rowId
		 */
		clearType: function ( context, rowId ) {
			// Get children
			const children = context.getters.getChildrenByParentRowId( rowId );
			// Exclude type key
			const keys = children.filter( ( row ) => row.key !== Constants.Z_OBJECT_TYPE );
			for ( const row of keys ) {
				context.dispatch( 'removeRowChildren', row.id );
				context.dispatch( 'removeRow', row.id );
			}
		}
	}
};