import { Archon } from "../Archon.js";
import { AddResourceOptions, Resource } from "../types/policy.js";

/**
 * This function creates a new Archon-managed resource for some object your software maintains. 
 * It returns an ARID that you can use to reference the resource in the future. You'll use the ARID to determine
 * access to the resource, so make sure you store it on your end.
 * @param options The options for the resource you're adding.
 * @returns The ARID of the newly created resource.
 */
export default async function addResource(options: AddResourceOptions): Promise<Resource> {
    // If the type is a ResourceType object, convert it to a ResourceTypeName.
    if (typeof options.type !== "string") {
        options.type = options.type.name;
    }

    const response = await Archon.request("/policy/resource", "POST", options);

    if (response.status !== 200) {
        throw new Error(`Failed to add resource: ${response.data}`);
    }

    return response.data;
}