{"version":3,"sources":["converters/seek.ts"],"names":[],"mappings":"AACA,wBAAgB,IAAI,CAAC,CAAC,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC,GAAG,CAAC,EAAE,EAMjD,MAAM,EAAE,CAAC,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,CAAC,GAAG,SAAS,KAAK,CAAC,EAAE,GAAG;IACpE,KAAK,EAAE,CAAC,EAAE,CAAC;IACX,OAAO,EAAE,OAAO,CAAC;CACpB,GAAG,SAAS,GAAG,KAAK,EAAE,IAAI,EAAE,CAAC,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,CAAC,GAAG,SAAS,EAAE,YAAY,EAAE,KAAK,EAAE,KAAK,KAAK,GAAG,SAAS,GAAG,KAAK,GAAG,KAAK,EAAE,CAwGhJ","file":"seek.d.ts","sourcesContent":["/** Create tree seeker */\nexport function seek<T, TData>(\n\trootNodes: T | T[],\n\t/**\n\t * @return  T[]\t- List of children\n\t * @return  undefined\t- No child or already created\n\t * @return  false\t- ignore this field\n\t */\n\tgoDown: (node: T, isInput: boolean, parentNode: T | undefined) => T[] | { nodes: T[], isInput: boolean } | undefined | false,\n\tgoUp: (node: T, isInput: boolean, parentNode: T | undefined, childrenData: TData[]) => TData | undefined | false\n): TData[] {\n\tconst rootChildrenData: TData[] = [];\n\tconst queue: QueueSchema<T, TData>[] = [\n\t\t{\n\t\t\tstate: NodeVisitState.ROOT_NODE,\n\t\t\tchildrenData: rootChildrenData\n\t\t}\n\t];\n\t//* Add root nodes\n\tif (!Array.isArray(rootNodes)) rootNodes = [rootNodes];\n\tfor (let i = 0, len = rootNodes.length; i < len; ++i) {\n\t\tlet nodeData: TData[] = []\n\t\tqueue.push({\n\t\t\tstate: NodeVisitState.COLLECT_DATA,\n\t\t\tchildrenData: rootChildrenData,\n\t\t\tindex: i\n\t\t}, {\n\t\t\tnode: rootNodes[i],\n\t\t\tisInput: false,\n\t\t\tparentNode: undefined,\n\t\t\tstate: NodeVisitState.GO_UP,\n\t\t\tchildrenData: nodeData\n\t\t}, {\n\t\t\tnode: rootNodes[i],\n\t\t\tisInput: false,\n\t\t\tparentNode: undefined,\n\t\t\tstate: NodeVisitState.GO_DOWN,\n\t\t\tchildrenData: nodeData\n\t\t});\n\t}\n\t// Seek\n\tconst errors: string[] = []\n\tvar result: TData[];\n\tvar childReturnedData: TData | undefined | false;\n\tconst _isArray = Array.isArray;\n\trootLoop: while (true) {\n\t\ttry {\n\t\t\tconst item = queue.pop()!;\n\t\t\tconst { childrenData, state } = item;\n\t\t\tswitch (state) {\n\t\t\t\tcase NodeVisitState.GO_DOWN: {\n\t\t\t\t\tlet { node, parentNode, isInput } = item;\n\t\t\t\t\tlet childNodes = goDown(node, isInput, parentNode);\n\t\t\t\t\tif (childNodes == null || childNodes === false) break; // if circular or already created or ignore it\n\t\t\t\t\tif (!_isArray(childNodes)) {\n\t\t\t\t\t\tisInput = childNodes.isInput;\n\t\t\t\t\t\tchildNodes = childNodes.nodes;\n\t\t\t\t\t}\n\t\t\t\t\t//* Go through children\n\t\t\t\t\tfor (let i = 0, len = childNodes.length; i < len; ++i) {\n\t\t\t\t\t\tlet childData: TData[] = [];\n\t\t\t\t\t\tqueue.push({\n\t\t\t\t\t\t\tstate: NodeVisitState.COLLECT_DATA,\n\t\t\t\t\t\t\tchildrenData: childrenData,\n\t\t\t\t\t\t\tindex: i\n\t\t\t\t\t\t}, {\n\t\t\t\t\t\t\tnode: childNodes[i],\n\t\t\t\t\t\t\tisInput,\n\t\t\t\t\t\t\tparentNode: node,\n\t\t\t\t\t\t\tstate: NodeVisitState.GO_UP,\n\t\t\t\t\t\t\tchildrenData: childData\n\t\t\t\t\t\t}, {\n\t\t\t\t\t\t\tnode: childNodes[i],\n\t\t\t\t\t\t\tisInput,\n\t\t\t\t\t\t\tparentNode: node,\n\t\t\t\t\t\t\tstate: NodeVisitState.GO_DOWN,\n\t\t\t\t\t\t\tchildrenData: childData\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tcase NodeVisitState.COLLECT_DATA: {\n\t\t\t\t\t// @ts-ignore\n\t\t\t\t\tchildrenData[item.index] = childReturnedData;\n\t\t\t\t\t// childrenData.push(childReturnedData);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tcase NodeVisitState.GO_UP: {\n\t\t\t\t\tlet { node, parentNode, isInput } = item;\n\t\t\t\t\tchildReturnedData = goUp(node, isInput, parentNode, childrenData);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tcase NodeVisitState.ROOT_NODE: {\n\t\t\t\t\t//* The end of seek\n\t\t\t\t\tresult = childrenData;\n\t\t\t\t\tbreak rootLoop;\n\t\t\t\t}\n\t\t\t\tdefault: {\n\t\t\t\t\tlet c: never = state;\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tif (typeof error === 'string') errors.push(error);\n\t\t\telse throw error;\n\t\t}\n\t}\n\tif (errors.length != 0) throw new Error(\"Errors:\\n• \" + errors.join(\"\\n• \"));\n\treturn result;\n}\n\n/** Node Visit state */\nenum NodeVisitState {\n\tGO_DOWN,\n\tCOLLECT_DATA,\n\tGO_UP,\n\t/** Used to collect last data to be returned by the function */\n\tROOT_NODE\n}\n\n/** Queue schema */\ntype QueueSchema<T, TData> = QueueSchemaSeek<T, TData> | QueueSchemaCollect<TData> | QueueRootNode<TData>;\n\n/** Go Up and Down */\ninterface QueueSchemaSeek<T, TData> {\n\t/** Current node */\n\tnode: T,\n\t/** Is input */\n\tisInput: boolean\n\t/** Parent node */\n\tparentNode: T | undefined\n\t/** Current visit state */\n\tstate: NodeVisitState.GO_DOWN | NodeVisitState.GO_UP,\n\t/** Data from child nodes */\n\tchildrenData: TData[]\n}\n\n/** Collect data */\ninterface QueueSchemaCollect<TData> {\n\tstate: NodeVisitState.COLLECT_DATA,\n\t/** Data from child nodes */\n\tchildrenData: TData[],\n\t/** Node index */\n\tindex: number\n}\n/** Root node */\ninterface QueueRootNode<TData> {\n\tstate: NodeVisitState.ROOT_NODE\n\t/** Data from child nodes */\n\tchildrenData: TData[]\n}"]}