{"version":3,"file":"tethys-cdk-immutable.mjs","sources":["../../../cdk/immutable/object-producer.ts","../../../cdk/immutable/immutable.ts","../../../cdk/immutable/tethys-cdk-immutable.ts"],"sourcesContent":["type GetIndexedField<T, K> = K extends keyof T\n    ? T[K]\n    : K extends `${number}`\n      ? '0' extends keyof T\n          ? undefined\n          : number extends keyof T\n            ? T[number]\n            : undefined\n      : undefined;\ntype FieldWithPossiblyUndefined<T, Key> = GetFieldType<Exclude<T, undefined>, Key> | Extract<T, undefined>;\ntype IndexedFieldWithPossiblyUndefined<T, Key> = GetIndexedField<Exclude<T, undefined>, Key> | Extract<T, undefined>;\nexport type GetFieldType<T, P> = P extends `${infer Left}.${infer Right}`\n    ? Left extends keyof T\n        ? FieldWithPossiblyUndefined<T[Left], Right>\n        : Left extends `${infer FieldKey}[${infer IndexKey}]`\n          ? FieldKey extends keyof T\n              ? FieldWithPossiblyUndefined<IndexedFieldWithPossiblyUndefined<T[FieldKey], IndexKey>, Right>\n              : undefined\n          : undefined\n    : P extends keyof T\n      ? T[P]\n      : P extends `${infer FieldKey}[${infer IndexKey}]`\n        ? FieldKey extends keyof T\n            ? IndexedFieldWithPossiblyUndefined<T[FieldKey], IndexKey>\n            : undefined\n        : undefined;\n\nexport class ObjectProducer<TEntity> {\n    entity: TEntity;\n\n    constructor(entity: TEntity) {\n        this.entity = entity;\n    }\n\n    get<TPath extends string>(propPath: TPath): GetFieldType<TEntity, TPath> {\n        return propPath.split('.').reduce((previousValue: any, part: string) => previousValue && previousValue[part], this.entity);\n    }\n\n    set<TPath extends string>(propPath: TPath, value: GetFieldType<TEntity, TPath>) {\n        const newEntity = { ...this.entity };\n\n        const split = propPath.split('.');\n        const lastIndex = split.length - 1;\n\n        split.reduce((previousValue: any, part, index) => {\n            if (index === lastIndex) {\n                previousValue[part] = value;\n            } else {\n                previousValue[part] = Array.isArray(previousValue[part]) ? previousValue[part].slice() : { ...previousValue[part] };\n            }\n\n            return previousValue && previousValue[part];\n        }, newEntity);\n\n        this.entity = newEntity;\n        return newEntity;\n    }\n}\n","import { coerceArray } from '@angular/cdk/coercion';\nimport { isFunction, isUndefinedOrNull, isArray } from '@tethys/cdk/is';\nimport { ObjectProducer } from './object-producer';\n\nexport type Id = string | number;\n\nexport type Ids = Id[];\n\nexport type IdOrIds = Id | Ids;\n\nexport interface EntityAddOptions {\n    prepend?: boolean;\n\n    afterId?: Id;\n}\n\nexport interface EntityMoveOptions {\n    afterId?: Id;\n\n    toIndex?: number;\n}\n\ntype ExtractKeysOfValueType<T, K> = { [I in keyof T]: T[I] extends K ? I : never }[keyof T];\nexport interface ProducerOptions<TEntity> {\n    idKey?: ExtractKeysOfValueType<TEntity, Id>;\n}\n\nexport class Producer<TEntity> {\n    private idKey = '_id';\n\n    private entities: TEntity[];\n\n    constructor(entities: TEntity[], options?: ProducerOptions<TEntity>) {\n        this.entities = entities || [];\n        if (options && options.idKey) {\n            this.idKey = options.idKey as string;\n        }\n    }\n\n    private getPathValue(entity: TEntity, path: string = this.idKey): string | number {\n        return (entity as Record<string, unknown>)[path] as string | number ;\n    }\n\n    /**\n     * Add an entity or entities.\n     *\n     * @example\n     * produce([users]).add(Entity);\n     * produce([users]).add([Entity, Entity]);\n     * produce([users]).add(Entity, { prepend: true });\n     * produce([users]).add(Entity, { afterId: '' });\n     */\n    add(entity: TEntity | TEntity[], addOptions?: EntityAddOptions): TEntity[] {\n        const addEntities = coerceArray(entity);\n        if (addEntities.length === 0) {\n            return this.entities;\n        }\n        if (addOptions && (addOptions.afterId || addOptions.prepend)) {\n            if (addOptions.afterId) {\n                const entities = [...this.entities];\n                const index =\n                    this.entities.findIndex(item => {\n                        return this.getPathValue(item) === addOptions.afterId;\n                    }) + 1;\n                entities.splice(index, 0, ...addEntities);\n                this.entities = [...entities];\n            } else if (addOptions.prepend) {\n                this.entities = [...addEntities, ...this.entities];\n            }\n        } else {\n            this.entities = [...this.entities, ...addEntities];\n        }\n        return this.entities;\n    }\n\n    /**\n     *\n     * Update an entity or entities.\n     *\n     * @example\n     * produce([users]).update(3, {\n     *   name: 'New Name'\n     * });\n     *\n     * produce([users]).update(3, entity => {\n     *    return {\n     *      ...entity,\n     *      name: 'New Name'\n     *    }\n     *  });\n     *\n     * produce([users]).update([1,2,3], {\n     *   name: 'New Name'\n     * });\n     */\n    update(id: IdOrIds | null, newStateFn: (entity: Readonly<TEntity>) => Partial<TEntity>): TEntity[];\n    update(id: IdOrIds | null, newState?: Partial<TEntity>): TEntity[];\n    update(\n        idsOrFn: IdOrIds | null | Partial<TEntity> | ((entity: Readonly<TEntity>) => boolean),\n        newStateOrFn?: ((entity: Readonly<TEntity>) => Partial<TEntity>) | Partial<TEntity>\n    ): TEntity[] {\n        const ids = coerceArray(idsOrFn);\n\n        for (let i = 0; i < this.entities.length; i++) {\n            const oldEntity = this.entities[i];\n            if (ids.indexOf(this.getPathValue(oldEntity)) >= 0) {\n                const newState = isFunction(newStateOrFn) ? newStateOrFn(oldEntity) : newStateOrFn;\n                this.entities[i] = { ...oldEntity, ...newState };\n            }\n        }\n        return [...this.entities];\n    }\n\n    /**\n     *\n     * Remove one or more entities:\n     *\n     * @example\n     * produce([users]).remove(5);\n     * produce([users]).remove([1,2,3]);\n     * produce([users]).remove(entity => entity.id === 1);\n     */\n    remove(id: IdOrIds): TEntity[];\n    remove(predicate: (entity: Readonly<TEntity>) => boolean): TEntity[];\n    remove(idsOrFn?: IdOrIds | ((entity: Readonly<TEntity>) => boolean)): TEntity[] {\n        if (isFunction(idsOrFn)) {\n            this.entities = this.entities.filter(entity => {\n                return !(idsOrFn as any)(entity);\n            });\n        } else {\n            const ids = coerceArray(idsOrFn);\n            this.entities = this.entities.filter(entity => {\n                return ids.indexOf(this.getPathValue(entity)) === -1;\n            });\n        }\n        return this.entities;\n    }\n\n    /**\n     *\n     * Move one or more entities:\n     *\n     * @example\n     * produce([users]).move(5, {afterId: 2});\n     * produce([users]).move(5, {toIndex: 0});\n     */\n    move(id: Id, moveOptions?: EntityMoveOptions): TEntity[] {\n        const fromIndex = this.entities.findIndex(item => this.getPathValue(item) === id);\n        let toIndex = 0;\n        const newEntities = [...this.entities];\n\n        if (!id || fromIndex < 0) {\n            return [...this.entities];\n        }\n\n        if (moveOptions) {\n            if (!isUndefinedOrNull(moveOptions.afterId)) {\n                toIndex = this.entities.findIndex(item => this.getPathValue(item) === moveOptions.afterId);\n            } else if (moveOptions.toIndex) {\n                toIndex = moveOptions.toIndex;\n            }\n        }\n        toIndex = Math.max(0, Math.min(this.entities.length - 1, toIndex));\n        if (toIndex === fromIndex) {\n            return [...this.entities];\n        } else {\n            const target = this.entities[fromIndex];\n            const delta = toIndex < fromIndex ? -1 : 1;\n\n            for (let i = fromIndex; i !== toIndex; i += delta) {\n                newEntities[i] = newEntities[i + delta];\n            }\n            newEntities[toIndex] = target;\n            return [...newEntities];\n        }\n    }\n}\n\nexport function produce<TEntity>(entities: TEntity[], options?: ProducerOptions<TEntity>): Producer<TEntity>;\nexport function produce<TEntity>(entities: TEntity): ObjectProducer<TEntity>;\nexport function produce<TEntity>(entities: TEntity | TEntity[], options?: ProducerOptions<TEntity>) {\n    if (isArray(entities)) {\n        return new Producer<TEntity>(entities, options);\n    } else {\n        return new ObjectProducer<TEntity>(entities);\n    }\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;MA2Ba,cAAc,CAAA;AAGvB,IAAA,WAAA,CAAY,MAAe,EAAA;AACvB,QAAA,IAAI,CAAC,MAAM,GAAG,MAAM;IACxB;AAEA,IAAA,GAAG,CAAuB,QAAe,EAAA;QACrC,OAAO,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,aAAkB,EAAE,IAAY,KAAK,aAAa,IAAI,aAAa,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,MAAM,CAAC;IAC9H;IAEA,GAAG,CAAuB,QAAe,EAAE,KAAmC,EAAA;QAC1E,MAAM,SAAS,GAAG,EAAE,GAAG,IAA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