import { whenRequest } from '@wix/fe-essentials/http-client/testkit/client';
import { Host, } from '@wix/bex-utils/@wix/ambassador-app-settings-v1-settings/types';
import { getGlobalSettings, setGlobalSettings, } from '@wix/bex-utils/@wix/ambassador-app-settings-v1-settings/http';
import { aGetGlobalSettingsResponse, aSetGlobalSettingsResponse, } from '@wix/bex-utils/@wix/ambassador-app-settings-v1-settings/builders';
export class ViewsServiceTestkit {
    constructor({ namespace }) {
        this.mocks = [];
        this.lastRequest = {
            views: [],
            hiddenPresetIds: [],
        };
        this.namespace = namespace;
    }
    getNamespace() {
        return `${this.namespace}.views`;
    }
    shouldPersist(repeat) {
        return repeat === 0;
    }
    addMock(repeat = 0, cb) {
        this.shouldPersist(repeat)
            ? this.mocks.push(cb().persist())
            : this.mocks.push(...Array.from(Array(repeat)).map(cb));
    }
    getMocks() {
        return this.mocks;
    }
    mockFetchViews({ payload, options = {}, }) {
        const { views = this.lastRequest.views || [], hiddenPresetIds = this.lastRequest.hiddenPresetIds || [], } = payload;
        this.lastRequest = {
            views,
            hiddenPresetIds,
        };
        const { repeat = 0 } = options;
        const shouldPersist = this.shouldPersist(repeat);
        this.addMock(options?.repeat, () => whenRequest(getGlobalSettings)
            .withData((data) => data.__CAIRO_VIEWS_TEST === 'true')
            .reply(() => ({
            status: 200,
            data: aGetGlobalSettingsResponse({
                settings: {
                    [this.getNamespace()]: {
                        views: shouldPersist ? this.lastRequest.views : views,
                        hiddenPresetIds: shouldPersist
                            ? this.lastRequest.hiddenPresetIds
                            : hiddenPresetIds,
                    },
                },
            }),
        })));
    }
    mockUpdateViews({ payload, options }) {
        const { views = [] } = payload;
        this.addMock(options?.repeat, () => whenRequest(setGlobalSettings)
            .withData((data) => {
            const viewsData = data?.settings?.[this.getNamespace()];
            this.mockFetchViews({ payload: viewsData, options });
            options?.stub?.(viewsData);
            const noViews = !viewsData.views.length && !views.length;
            return (data.host === Host.BUSINESS_MANAGER &&
                data.__CAIRO_VIEWS_TEST === 'true' &&
                (noViews ||
                    viewsData.views.some((view1) => views.some((view2) => view1.name === view2.name))));
        })
            .reply(() => ({
            status: 200,
            data: aSetGlobalSettingsResponse({
                settings: {
                    [this.getNamespace()]: {
                        views,
                    },
                },
            }),
        })));
    }
    mockUpdateHiddenPreset({ payload, options, }) {
        const { hiddenPresetIds = [] } = payload;
        this.addMock(options?.repeat, () => whenRequest(setGlobalSettings)
            .withData((data) => {
            this.mockFetchViews({
                payload: { views: [], hiddenPresetIds },
                options,
            });
            options?.stub?.(data?.settings?.[this.getNamespace()]);
            return (data.__CAIRO_VIEWS_TEST === 'true' &&
                data?.settings[`${this.namespace}.views`].hiddenPresetIds.some((hiddenPresetId1) => hiddenPresetIds.some((hiddenPresetId2) => hiddenPresetId1 === hiddenPresetId2)));
        })
            .reply(() => ({
            status: 200,
            data: aSetGlobalSettingsResponse({
                settings: {
                    [this.getNamespace()]: {
                        hiddenPresetIds,
                    },
                },
            }),
        })));
    }
}
//# sourceMappingURL=viewsServiceTestkit.js.map