import { WixPatternsContainerDriver, } from './WixPatternsContainer.driver';
import { CollectionState } from '../state';
import { createProductsBackend } from './createProductsBackend';
import { parseFqdn } from '../util';
export class ProductsCollectionStateDriver {
    constructor(overrides = {}) {
        this.fetchDataStartSpy = () => {
            const spy = jest.fn();
            this.backend.events.on('fetchDataStart', spy);
            return spy;
        };
        this.overrides = overrides;
        this.container = new WixPatternsContainerDriver(overrides);
        this.backend = createProductsBackend(overrides.backend);
        const queryName = 'wix.cairo.products.v1.product';
        const fqdn = overrides.fqdn
            ? parseFqdn(overrides.fqdn)
            : parseFqdn(queryName);
        this.collection = new CollectionState({
            queryName,
            fqdn,
            toExtendedFields: (item) => item.extendedFields,
            ...(overrides.withFetchTotal && {
                fetchTotal: (query) => {
                    return this.backend.fetchTotal(query);
                },
            }),
            fetchData: (query) => this.backend.fetchData(query),
            itemKey: (item) => item.id,
            itemName: (item) => item.name,
            filters: {},
            limit: 50,
            onlineState: this.container.onlineState,
            translate: this.container.i18n.t.bind(this.container.i18n),
            queryCache: this.container.queryCache,
            history: this.container.history,
            errorMonitor: this.container.errorMonitor,
            errorHandler: this.container.errorHandler,
            showToast: this.container.showToast,
            initDependencies: () => this.container.container.initialFetch(),
            lodash: this.container.container.lodash,
            ...overrides.collection,
        });
    }
    reCreateCollection() {
        const { queryName, query: { limit }, filters, } = this.collection;
        this.collection = new CollectionState({
            queryName,
            fqdn: parseFqdn(queryName),
            fetchData: (query) => this.backend.fetchData(query),
            itemKey: (item) => item.id,
            itemName: (item) => item.name,
            filters,
            limit,
            onlineState: this.container.onlineState,
            translate: this.container.i18n.t.bind(this.container.i18n),
            queryCache: this.container.queryCache,
            history: this.container.history,
            errorMonitor: this.container.errorMonitor,
            errorHandler: this.container.errorHandler,
            showToast: this.container.showToast,
            initDependencies: () => this.container.container.initialFetch(),
            lodash: this.container.container.lodash,
            ...this.overrides.collection,
        });
    }
    getRandomItem() {
        const { items } = this;
        const index = this.backend.chance.integer({
            min: 0,
            max: items.length - 1,
        });
        return {
            index,
            item: items[index],
        };
    }
    get items() {
        const { collection: { query }, } = this;
        const { items } = this.backend.fetchDataSync(query.asComputed);
        return items;
    }
    get chance() {
        return this.backend.chance;
    }
}
//# sourceMappingURL=ProductsCollectionState.driver.js.map