import { withoutDefaults } from '../util';
import { cairoChangesBeforeApply, cairoDataExAddCustomField, cairoDataExCustomFieldAdvancesSettings, cairoDataExCustomFieldEndProcess, cairoDataExtensionUnsuccessfulUpdateInServer, cairoLearnMore, } from '@wix/bex-utils/@wix/bi-logger-os-data/v2';
import { reaction } from 'mobx';
import { formatPermissionsForBi } from './formatPermissionsForBi';
export class CustomFieldModalBiReporter {
    constructor(params) {
        this.formSnapshot = null;
        this.reportAddCustomField = ({ origin, customOrigin, }) => {
            this.reportBi(withoutDefaults(cairoDataExAddCustomField)({
                origin,
                customOrigin,
            }));
        };
        this.reportAdvancedSettingsClick = (actionName) => {
            this.reportBi(withoutDefaults(cairoDataExCustomFieldAdvancesSettings)({
                origin: this.origin,
                actionName,
            }));
        };
        this.reportLearnMoreClick = () => {
            this.reportBi(withoutDefaults(cairoLearnMore)({
                origin: this.origin,
            }));
        };
        this.reportModalEndProcess = (params) => {
            const { endType } = params;
            const { $: { name, type, key }, } = this.form;
            const { customFieldModalState: { origin, customOrigin }, } = this;
            this.reportBi(withoutDefaults(cairoDataExCustomFieldEndProcess)({
                actionName: this.mode === 'new' ? 'Add' : 'Edit',
                endType,
                fieldName: name.value,
                fieldType: type.value,
                key: key.value,
                numAdded: this.customFieldModalState.fieldCount(),
                changedFields: this.getBiChangedFields(),
                permissions: this.getBiPermissions(),
                origin,
                customOrigin,
            }));
        };
        this.reportSubmitFailure = (params) => {
            const { customFieldModalState } = this;
            const { type, name, key } = customFieldModalState.getFormValues();
            this.reportBi(withoutDefaults(cairoDataExtensionUnsuccessfulUpdateInServer)({
                actionName: this.mode === 'new' ? 'Add' : 'Edit',
                errorType: params.errorType,
                origin: customFieldModalState.origin,
                permissions: this.getBiPermissions(),
                fieldType: type,
                fieldName: name,
                key,
            }));
        };
        this.customFieldModalState = params.customFieldModalState;
        this.reportTypeFormFieldChange = this.createReportFormFieldChange({
            fieldName: 'Type',
            fieldValue: () => this.form.$.type.value,
        });
        this.reportNameFormFieldChange = this.createReportFormFieldChange({
            fieldName: 'Name',
            fieldValue: () => this.form.$.name.value,
        });
        this.reportKeyFormFieldChange = this.createReportFormFieldChange({
            fieldName: 'Key',
            fieldValue: () => this.form.$.key.value,
        });
        this.reportPermissionFormFieldChange = this.createReportFormFieldChange({
            fieldName: 'Permission',
            fieldValue: () => this.getBiPermissions(),
        });
    }
    get reportBi() {
        return this.customFieldModalState.reportBi;
    }
    get form() {
        return this.customFieldModalState.form;
    }
    get mode() {
        return this.customFieldModalState.mode;
    }
    get origin() {
        return this.mode === 'new'
            ? 'Add Custom Field Modal'
            : 'Edit Custom Field Modal';
    }
    createReportFormFieldChange({ fieldName, fieldValue, }) {
        const { lodash: { debounce }, } = this.customFieldModalState.container;
        return debounce(() => this.reportBi(withoutDefaults(cairoChangesBeforeApply)({
            feature: this.mode === 'new' ? 'Add Custom Field' : 'Edit Custom Field',
            fieldName,
            fieldValue: fieldValue(),
            isClearButton: false,
        })), 1500, { leading: false, trailing: true });
    }
    getBiChangedFields() {
        const prevValues = this.formSnapshot;
        if (this.mode === 'new' || !prevValues) {
            return undefined;
        }
        const nextValues = this.customFieldModalState.getFormValues();
        const changes = [];
        if (prevValues.name !== nextValues.name) {
            changes.push('Name');
        }
        if (prevValues.readApps !== nextValues.readApps ||
            prevValues.readUsers !== nextValues.readUsers ||
            prevValues.writeApps !== nextValues.writeApps ||
            prevValues.writeUsers !== nextValues.writeUsers) {
            changes.push('Permission');
        }
        if (changes.length !== 0) {
            return changes.join(',');
        }
        return 'No changes';
    }
    getBiPermissions() {
        return formatPermissionsForBi(this.customFieldModalState.getFormValues());
    }
    init() {
        const { customFieldModalState: { form }, } = this;
        const disposes = [
            reaction(() => form.$.name.value, () => {
                this.reportNameFormFieldChange();
            }),
            reaction(() => form.$.type.value, () => {
                this.reportTypeFormFieldChange();
            }),
            reaction(() => ({
                readUsers: form.$.readUsers.value,
                readApps: form.$.readApps.value,
                writeUsers: form.$.writeUsers.value,
                writeApps: form.$.writeApps.value,
            }), () => {
                this.reportPermissionFormFieldChange();
            }),
        ];
        return () => {
            disposes.forEach((disposer) => {
                disposer?.();
            });
        };
    }
}
//# sourceMappingURL=CustomFieldModalBiReporter.js.map