import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2019-2026 GoodData Corporation
import { cloneDeep } from "lodash-es";
import { MeasureGroupIdentifier, bucketAttributes, insightBucket, newDimension, } from "@gooddata/sdk-model";
import { BucketNames } from "@gooddata/sdk-ui";
import { CoreXirr, updateConfigWithSettings } from "@gooddata/sdk-ui-charts";
import { getXirrBuckets } from "./xirrBucketHelper.js";
import { removeAllArithmeticMeasuresFromDerived, removeAllDerivedMeasures, sanitizeFilters, } from "../../../utils/bucketHelper.js";
import { hasGlobalDateFilter } from "../../../utils/bucketRules.js";
import { getReferencePointWithSupportedProperties, getSupportedProperties, } from "../../../utils/propertiesHelper.js";
import { removeSort } from "../../../utils/sort.js";
import { getDefaultXirrUiConfig, getXirrUiConfig, } from "../../../utils/uiConfigHelpers/xirrUiConfigHelper.js";
import { UnsupportedConfigurationPanel } from "../../configurationPanels/UnsupportedConfigurationPanel.js";
import { AbstractPluggableVisualization } from "../AbstractPluggableVisualization.js";
/**
 * PluggableXirr
 *
 * ## Buckets
 *
 * | Name           | Id        | Accepts       |
 * |----------------|-----------|---------------|
 * | Measure        | measures  | measures only |
 * | Date Attribute | attribute | dates only    |
 *
 * ### Bucket axioms
 *
 * - |Measure| = 1
 * - |DateAttribute| = 1
 *
 * ## Dimensions
 *
 * The PluggableXirr always creates one dimensional execution.
 *
 * - ⊤ ⇒ [[MeasureGroupIdentifier, DateAttribute]]
 *
 * ## Sorts
 *
 * The PluggableXirr does not use any sorts.
 */
export class PluggableXirr extends AbstractPluggableVisualization {
    settings;
    renderFun;
    unmountFun;
    constructor(props) {
        super(props);
        this.settings = props.featureFlags;
        this.renderFun = props.renderFun;
        this.unmountFun = props.unmountFun;
    }
    unmount() {
        this.unmountFun([this.getElement(), this.getConfigPanelElement()]);
    }
    getExtendedReferencePoint = async (referencePoint) => {
        const referencePointCloned = cloneDeep(referencePoint);
        let newReferencePoint = {
            ...referencePointCloned,
            uiConfig: getDefaultXirrUiConfig(),
        };
        if (!hasGlobalDateFilter(referencePoint.filters)) {
            newReferencePoint = removeAllArithmeticMeasuresFromDerived(newReferencePoint);
            newReferencePoint = removeAllDerivedMeasures(newReferencePoint);
        }
        const buckets = getXirrBuckets(referencePoint);
        newReferencePoint.buckets = buckets;
        newReferencePoint.uiConfig = getXirrUiConfig(newReferencePoint, this.intl);
        newReferencePoint = removeSort(newReferencePoint);
        newReferencePoint = getReferencePointWithSupportedProperties(newReferencePoint, this.supportedPropertiesList);
        return Promise.resolve(sanitizeFilters(newReferencePoint, this.featureFlags?.enableImprovedAdFilters, referencePointCloned));
    };
    getExecution(options, insight, executionFactory) {
        const { dateFormat } = options;
        return executionFactory
            .forInsight(insight)
            .withDimensions(...this.getXirrDimensions(insight))
            .withDateFormat(dateFormat);
    }
    renderVisualization(options, insight, executionFactory) {
        const { locale, custom = {}, config } = options;
        const { drillableItems } = custom;
        const execution = this.getExecution(options, insight, executionFactory);
        this.renderFun(_jsx(CoreXirr, { enableExecutionCancelling: config?.enableExecutionCancelling ?? false, execution: execution, drillableItems: drillableItems, onDrill: this.onDrill, locale: locale, config: updateConfigWithSettings(config, this.settings), afterRender: this.afterRender, onLoadingChanged: this.onLoadingChanged, pushData: this.pushData, onError: this.onError, LoadingComponent: undefined, ErrorComponent: undefined }), this.getElement());
    }
    renderConfigurationPanel() {
        const configPanelElement = this.getConfigPanelElement();
        if (configPanelElement) {
            const properties = this.visualizationProperties ?? {};
            this.renderFun(_jsx(UnsupportedConfigurationPanel, { locale: this.locale, pushData: this.pushData, properties: getSupportedProperties(properties, this.supportedPropertiesList) }), configPanelElement);
        }
    }
    getXirrDimensions(insight) {
        const attributeBucket = insightBucket(insight, BucketNames.ATTRIBUTE);
        const attributes = attributeBucket ? bucketAttributes(attributeBucket) : [];
        return [newDimension([MeasureGroupIdentifier, ...attributes])];
    }
    withEmptyAttributeTargets(data) {
        return {
            ...data,
            availableDrillTargets: {
                ...data?.availableDrillTargets,
                attributes: [],
            },
        };
    }
    // This is effectively calling super.pushData()
    // https://stackoverflow.com/questions/31088947/inheritance-method-call-triggers-typescript-compiler-error
    // https://github.com/basarat/typescript-book/blob/master/docs/arrow-functions.md#tip-arrow-functions-and-inheritance
    // with new TS we got Property 'handlePushData' is used before its initialization.ts(2729)
    // it is not possible to call super.handlePushData() directly and get reference to this.handlePushData in constructor
    // tested in runtime and it works
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-ignore
    superPushData = this.pushData;
    pushData = (data, options) => {
        // For xirr chart we do not support drilling from attributes.
        const filterAttributes = this.withEmptyAttributeTargets(data);
        this.superPushData(filterAttributes, options);
    };
}
//# sourceMappingURL=PluggableXirr.js.map