import { Resource, ResourceKind } from "../resource.js";
import { logger } from "../util/logger.js";
import { CloudflareApiError, handleApiError } from "./api-error.js";
import { createCloudflareApi, } from "./api.js";
export function isVectorizeMetadataIndex(resource) {
    return resource?.[ResourceKind] === "cloudflare::VectorizeMetadataIndex";
}
/**
 * Creates and manages Cloudflare Vectorize Metadata Indexes.
 *
 * Vectorize Metadata Indexes enable filtering based on metadata properties when querying vectors.
 * Each Vectorize Index can have up to 10 metadata indexes.
 *
 * @example
 * // First create a Vectorize Index
 * const vectorIndex = await VectorizeIndex("documents", {
 *   name: "documents",
 *   config: {
 *     dimensions: 768,
 *     metric: "cosine"
 *   }
 * });
 *
 * // Then create a metadata index for the "category" property
 * const categoryIndex = await VectorizeMetadataIndex("category-index", {
 *   index: vectorIndex,
 *   propertyName: "category",
 *   indexType: "string"
 * });
 *
 * @example
 * // Create a metadata index for a numeric property
 * const yearIndex = await VectorizeMetadataIndex("year-index", {
 *   index: vectorIndex,
 *   propertyName: "year",
 *   indexType: "number"
 * });
 *
 * @see https://developers.cloudflare.com/vectorize/
 */
export const VectorizeMetadataIndex = Resource("cloudflare::VectorizeMetadataIndex", async function (_id, props) {
    const api = await createCloudflareApi(props);
    const indexName = props.index.name;
    const propertyName = props.propertyName;
    if (this.phase === "delete") {
        // Delete metadata index
        try {
            await deleteMetadataIndex(api, indexName, propertyName);
        }
        catch (error) {
            if (error instanceof CloudflareApiError &&
                error.status === 400 &&
                error.message.includes("does not exist")) {
                // Index doesn't exist, which is what we want
            }
            else {
                throw error;
            }
        }
        return this.destroy();
    }
    if (this.phase === "update") {
        if (props.email === this.props.email &&
            props.index?.id === this.props.index?.id &&
            props.propertyName === this.props.propertyName &&
            props.indexType === this.props.indexType) {
            // Update operation is not supported
            if (!this.scope.quiet) {
                logger.warn(`Attempted to update Vectorize metadata index ${this.props.propertyName} but it was a no-op.`);
            }
            return this.output;
        }
        // Update operation is not supported
        throw new Error("Updating Vectorize metadata indexes is not supported by the Cloudflare API. " +
            "To change a metadata index, delete it and create a new one with the desired configuration.");
    }
    const indexData = await createMetadataIndex(api, indexName, props);
    return {
        id: propertyName, // Use propertyName as ID
        index: props.index,
        propertyName: props.propertyName,
        indexType: props.indexType,
        accountId: api.accountId,
        mutationId: indexData.result.mutationId,
    };
});
/**
 * Create a new Vectorize metadata index
 */
export async function createMetadataIndex(api, indexName, props) {
    // Create new metadata index
    const createPayload = {
        propertyName: props.propertyName,
        indexType: props.indexType,
    };
    const createResponse = await api.post(`/accounts/${api.accountId}/vectorize/v2/indexes/${indexName}/metadata_index/create`, createPayload);
    if (!createResponse.ok) {
        return await handleApiError(createResponse, "creating", "Vectorize metadata index", props.propertyName);
    }
    return (await createResponse.json());
}
/**
 * Delete a Vectorize metadata index
 */
export async function deleteMetadataIndex(api, indexName, propertyName) {
    const deleteResponse = await api.post(`/accounts/${api.accountId}/vectorize/v2/indexes/${indexName}/metadata_index/delete`, {
        propertyName,
    });
    if (!deleteResponse.ok) {
        await handleApiError(deleteResponse, "deleting", "Vectorize metadata index", propertyName);
    }
}
/**
 * List all metadata indexes for a Vectorize index
 */
export async function listMetadataIndexes(api, indexName) {
    const response = await api.get(`/accounts/${api.accountId}/vectorize/v2/indexes/${indexName}/metadata_index/list`);
    if (response.status === 410) {
        // Gone
        return [];
    }
    if (!response.ok) {
        throw new CloudflareApiError(`Failed to list metadata indexes: ${response.statusText}`, response);
    }
    const data = (await response.json());
    if (!data.success) {
        const errorMessage = data.errors?.[0]?.message || "Unknown error";
        throw new Error(`Failed to list metadata indexes: ${errorMessage}`);
    }
    return data.result.metadataIndexes;
}
//# sourceMappingURL=vectorize-metadata-index.js.map