// (C) 2022-2026 GoodData Corporation
import { bucketAttribute, idRef, insightBucket, insightProperties, insightVisualizationUrl, newAttribute, } from "@gooddata/sdk-model";
import { BucketNames } from "@gooddata/sdk-ui";
import { sdkModelPropMetas } from "../../../utils/embeddingCodeGenerator/insightToPropsConverter/convenience.js";
const supportedGeoConfigProperties = new Set([
    "center",
    "colorMapping",
    "cooperativeGestures",
    "legend",
    "limit",
    "selectedSegmentItems",
    "separators",
    "viewport",
    "points",
    "showLabels",
    "showLabels",
    "tooltipText",
]);
export function geoConfigFromInsight(insight, ctx) {
    const properties = insightProperties(insight);
    const controls = properties?.["controls"] ?? {};
    const withValuesFromContext = {
        ...controls,
        ...(ctx?.settings?.separators ? { separators: ctx?.settings?.separators } : {}),
    };
    const configFromProperties = Object.fromEntries(Object.entries(withValuesFromContext).filter(([key, value]) => supportedGeoConfigProperties.has(key) && !(value === null || value === undefined)));
    return {
        ...configFromProperties,
        ...mapBoxTokenPlaceholder(),
    };
}
export function geoInsightConversion(propName, bucketName) {
    return {
        propName,
        propType: sdkModelPropMetas.Attribute.Single,
        itemAccessor(insight, ctx) {
            return getLocationAttributeFromInsight(insight, ctx, bucketName);
        },
    };
}
function getLocationAttributeFromInsight(insight, ctx, bucketName) {
    if (bucketName === BucketNames.LOCATION &&
        !ctx?.backend?.capabilities.supportsSeparateLatitudeLongitudeLabels) {
        const bucket = insightBucket(insight, bucketName);
        return bucket ? bucketAttribute(bucket) : undefined;
    }
    else if (
    // dont rely on Latitude being already in bucket, take both lat and long from properties
    (bucketName === BucketNames.LATITUDE || bucketName === BucketNames.LONGITUDE) &&
        ctx?.backend?.capabilities.supportsSeparateLatitudeLongitudeLabels) {
        const properties = insightProperties(insight);
        const controls = properties?.["controls"] ?? {};
        const identifier = controls[bucketName];
        const localId = bucketName === BucketNames.LATITUDE ? "latitude_df" : "longitude_df";
        return newAttribute(idRef(identifier, "displayForm"), (a) => a.localId(localId));
    }
    return undefined;
}
export function mapBoxTokenPlaceholder() {
    return {
        mapboxToken: "<fill your Mapbox token here>",
    };
}
export function isGeoChart(insightDefinition) {
    const type = insightVisualizationUrl(insightDefinition).split(":")[1];
    return type === "pushpin";
}
export function geoConfigForInsightViewComponent() {
    return {
        value: mapBoxTokenPlaceholder(),
        meta: {
            cardinality: "scalar",
            typeImport: {
                importType: "named",
                name: "IGeoConfig",
                package: "@gooddata/sdk-ui-geo",
            },
        },
    };
}
//# sourceMappingURL=geoConfigCodeGenerator.js.map