import { addResizeObserver, AppsState, DataExtensionState, fqdnToString, TaskState, WixPatternsError, } from '@wix/bex-core';
import { SidePanelsState } from '../SidePanelsState';
import { action, computed, makeObservable, observable } from 'mobx';
import { cairoErrorInLoadingAComponent, cairoTryAgainClicked, loadEnd, loadStart, } from '@wix/bex-core/bi';
import { CustomFieldsPanelState } from './CustomFieldsPanelState';
export class CustomFieldsWidgetState {
    constructor({ container, fqdn, filterable, containsPii, defaultPermissions, extendedFields, enableArchive, }) {
        this.initTask = new TaskState();
        this._extendedFields = null;
        this._formState = null;
        this._widgetContentRef = {
            current: null,
        };
        this._customFieldsLazyComponents = undefined;
        this.container = container;
        this.fqdn = fqdn;
        this._extendedFields = extendedFields;
        this._size = 'default';
        this.panels = new SidePanelsState({
            panelGroupName: fqdnToString(fqdn),
            container,
        });
        this.reportBi = container.createBILogger({
            componentType: 'CustomFieldsWidget',
            ...fqdn,
        });
        this.dataExtension = new DataExtensionState({
            container,
            fqdn,
            closeSidePanel: this.panels.closeSidePanel,
            filterable,
            containsPii,
            defaultPermissions,
            enableArchive,
        });
        this.apps = new AppsState({
            container,
            dataExtension: this.dataExtension,
        });
        this.customFieldsPanel = new CustomFieldsPanelState({
            dataExtension: this.dataExtension,
            reportBi: this.reportBi,
            closeSidePanel: this.panels.closeSidePanel,
        });
        makeObservable(this, {
            hasWritePermissions: computed,
            widgetSize: computed,
            setWidgetContentRef: action,
            isLoading: computed,
            isError: computed,
            _size: observable.ref,
            _widgetContentRef: observable.ref,
            _extendedFields: observable.ref,
            _adjustWidgetSize: action.bound,
            retry: action.bound,
        });
    }
    get _userFieldsNamespace() {
        return this.dataExtension.dataExtensionService.userFieldsNamespace;
    }
    _appLoadingStart() {
        const startTime = performance.now();
        const commonBiParams = this._commonDynamicBiParams();
        this.reportBi(loadStart({
            ...commonBiParams,
        }));
        return () => {
            this.reportBi(loadEnd({
                ...commonBiParams,
                loadingTime: Math.round(performance.now() - startTime),
            }));
        };
    }
    init({ appLoaded }) {
        const { initTask, dataExtension, container, apps } = this;
        const disposers = [dataExtension.init(), apps.init()];
        initTask.runOnce(async () => {
            dataExtension.initTask.runOnce();
            apps.initTask.runOnce();
            const [lazyComponents] = await Promise.all([
                import(
                /* webpackChunkName: "CustomFieldsWidgetLazyContent" */
                '../../components/CustomFieldsWidget/CustomFieldsWidgetLazyContent').catch((err) => {
                    throw new WixPatternsError({
                        errorCode: 'LoadCustomFieldsWidgetLazyContentFailed',
                        originalException: err,
                    });
                }),
                dataExtension.initTask.status.promise?.catch((err) => {
                    throw new WixPatternsError({
                        errorCode: 'InitCustomFieldsDataExtensionFailed',
                        originalException: err,
                    });
                }),
                container.initTask.status.promise?.catch((err) => {
                    throw new WixPatternsError({
                        errorCode: 'InitCustomFieldsWidgetContainerFailed',
                        originalException: err,
                    });
                }),
                apps.initTask.status.promise?.catch(() => null),
            ]).catch((error) => {
                this.container.internalMonitor.reportError({
                    error,
                });
                this.reportBi(cairoErrorInLoadingAComponent({
                    errorType: error instanceof WixPatternsError ? error.name : undefined,
                }));
                throw error;
            });
            this._customFieldsLazyComponents = lazyComponents;
            appLoaded?.();
        });
        return () => {
            disposers.forEach((d) => d?.());
        };
    }
    getNamespaceFormSchemaAndData(namespace) {
        const { dataExtension, _extendedFields } = this;
        const { schema, fields } = dataExtension.getSchemaAndFieldsByNamespace(namespace);
        return {
            fields,
            schema,
            extendedFieldsDataMap: _extendedFields?.namespaces?.[namespace],
        };
    }
    initContent() {
        const disposers = [
            addResizeObserver(this._widgetContentRef.current, this._adjustWidgetSize.bind(this)),
        ];
        this._adjustWidgetSize();
        return () => {
            disposers.forEach((d) => d?.());
        };
    }
    _commonDynamicBiParams() {
        return {
            url: window.location.href,
            route: window.location.pathname,
        };
    }
    retry() {
        this.reportBi(cairoTryAgainClicked({
            actionName: 'Load custom fields',
            loaction: 'Custom fields widget',
        }));
        this.initTask.runOnce();
    }
    get isDirty() {
        return !!this._formState?.isDirty;
    }
    get widgetSize() {
        return this._size;
    }
    get isLoading() {
        const { status } = this.initTask;
        return status.isIdle || status.isLoading;
    }
    get ActionButtonsComponent() {
        return this._customFieldsLazyComponents?.ActionButtons;
    }
    get CustomFieldsWidgetContent() {
        return this._customFieldsLazyComponents?.CustomFieldsWidgetContent;
    }
    get WixPatternsModalsContainer() {
        return this._customFieldsLazyComponents?.WixPatternsModalsContainer;
    }
    get CustomFieldModal() {
        return this._customFieldsLazyComponents?.CustomFieldModal;
    }
    get ArchiveFieldModal() {
        return this._customFieldsLazyComponents?.ArchiveFieldModal;
    }
    setWidgetContentRef(ref) {
        this._widgetContentRef.current = ref;
    }
    _adjustWidgetSize() {
        if (this._widgetContentRef.current) {
            const element = this._widgetContentRef.current;
            if (element.clientWidth < 842) {
                this._size = 'small';
            }
            else {
                this._size = 'default';
            }
        }
    }
    get isError() {
        return this.initTask.status.isError;
    }
    async validate() {
        const data = this._formState?.validate();
        if (!data) {
            return {
                isValid: true,
            };
        }
        if (data?.isValid) {
            const extendedFields = data.extendedFields;
            return {
                isValid: true,
                extendedFields,
                values: extendedFields,
            };
        }
        return { isValid: false };
    }
    get hasWritePermissions() {
        return this.dataExtension.hasWritePermissions;
    }
}
//# sourceMappingURL=CustomFieldsWidgetState.js.map