import { EventTypes } from './EventTypes.js';
/**
 * Reads a user file definition from the store.
 * @param key The file key
 * @param media When true it reads file contents rather than metadata.
 */
function fileRead(key, media, target = document.body) {
    const e = new CustomEvent(EventTypes.Store.File.read, {
        bubbles: true,
        cancelable: true,
        composed: true,
        detail: {
            key,
            media,
            result: undefined,
        },
    });
    target.dispatchEvent(e);
    return e.detail.result;
}
/**
 * Patches a file in the store.
 * @param key The key of the file to patch
 * @param value The JSON patch to be processed.
 * @returns The JSON patch to revert the change using the `@api-client/json` library
 */
function patchFile(key, id, value, app, media, target = document.body) {
    const e = new CustomEvent(EventTypes.Store.File.patch, {
        bubbles: true,
        cancelable: true,
        composed: true,
        detail: {
            key,
            value,
            media,
            id,
            app,
            result: undefined,
        },
    });
    target.dispatchEvent(e);
    return e.detail.result;
}
export default Object.freeze({
    File: Object.freeze({
        /**
         * Lists files (spaces, projects, etc) in the store.
         *
         * @param kinds the list of kinds to list. Spaces are always included.
         * @param options Optional query options.
         */
        list: async (kinds, options, target = document.body) => {
            const e = new CustomEvent(EventTypes.Store.File.list, {
                bubbles: true,
                cancelable: true,
                composed: true,
                detail: {
                    kinds,
                    options,
                    result: undefined,
                },
            });
            target.dispatchEvent(e);
            return e.detail.result;
        },
        /**
         * Lists files that are shared with the user.
         *
         * @param kinds the list of kinds to list. Spaces are always included.
         * @param options Optional query options.
         */
        listShared: async (kinds, options, target = document.body) => {
            const e = new CustomEvent(EventTypes.Store.File.listShared, {
                bubbles: true,
                cancelable: true,
                composed: true,
                detail: {
                    kinds,
                    options,
                    result: undefined,
                },
            });
            target.dispatchEvent(e);
            return e.detail.result;
        },
        /**
         * Creates a file in the store.
         *
         * @param meta The definition of a file that extends the IFile interface.
         * @param media The file content to create with the file meta, if available.
         * @param opts Optional options when creating a file
         * @returns The key of the creates file.
         */
        create: async (meta, media, opts, target = document.body) => {
            const e = new CustomEvent(EventTypes.Store.File.create, {
                bubbles: true,
                cancelable: true,
                composed: true,
                detail: {
                    meta,
                    media,
                    opts,
                    result: undefined,
                },
            });
            target.dispatchEvent(e);
            return e.detail.result;
        },
        /**
         * A special case of a create file flow.
         * It allow to create a file only from a name and its kind.
         * The store has logic to recognize the file from its `kind` and create a file.
         *
         * This is helpful when creating a new file from the "create" dialog.
         *
         * @param name The name provided by the user.
         * @param kind File kind.
         * @param opts Optional options when creating a file
         * @returns The key of the creates file.
         */
        createDefault: async (name, kind, opts, target = document.body) => {
            const e = new CustomEvent(EventTypes.Store.File.createDefault, {
                bubbles: true,
                cancelable: true,
                composed: true,
                detail: {
                    name,
                    kind,
                    opts,
                    result: undefined,
                },
            });
            target.dispatchEvent(e);
            return e.detail.result;
        },
        read: fileRead,
        patch: patchFile,
        /**
         * Deletes the file in the store.
         *
         * @param key The key of the file to delete.
         */
        delete: async (key, target = document.body) => {
            const e = new CustomEvent(EventTypes.Store.File.delete, {
                bubbles: true,
                cancelable: true,
                composed: true,
                detail: {
                    key,
                    result: undefined,
                },
            });
            target.dispatchEvent(e);
            return e.detail.result;
        },
        /**
         * Updates the sharing options of the file.
         *
         * @param key The file key
         * @param id The generated by the client random id. This id is reported back by the server.
         * @param value The patch operation on the file's ACL
         */
        patchUsers: async (key, id, value, app, target = document.body) => {
            const e = new CustomEvent(EventTypes.Store.File.patchUsers, {
                bubbles: true,
                cancelable: true,
                composed: true,
                detail: {
                    key,
                    value,
                    id,
                    app,
                    result: undefined,
                },
            });
            target.dispatchEvent(e);
            await e.detail.result;
        },
        /**
         * Lists the users that have direct permission to the file.
         *
         * @param key The file key
         */
        listUsers: async (key, target = document.body) => {
            const e = new CustomEvent(EventTypes.Store.File.listUsers, {
                bubbles: true,
                cancelable: true,
                composed: true,
                detail: {
                    key,
                    result: undefined,
                },
            });
            target.dispatchEvent(e);
            return e.detail.result;
        },
        /**
         * Lists breadcrumbs to the file.
         *
         * @param fileOrFolder The if of the file or folder to query for.
         * @param options Optional query options.
         */
        breadcrumbs: async (fileOrFolder, target = document.body) => {
            const e = new CustomEvent(EventTypes.Store.File.breadcrumbs, {
                bubbles: true,
                cancelable: true,
                composed: true,
                detail: {
                    key: fileOrFolder,
                    result: undefined,
                },
            });
            target.dispatchEvent(e);
            return e.detail.result;
        },
    }),
    /**
     * Reads the current user.
     */
    User: Object.freeze({
        me: async (target = document.body) => {
            const e = new CustomEvent(EventTypes.Store.User.me, {
                bubbles: true,
                cancelable: true,
                composed: true,
                detail: {
                    result: undefined,
                },
            });
            target.dispatchEvent(e);
            return e.detail.result;
        },
        read: async (organizationId, key, target = document.body) => {
            const e = new CustomEvent(EventTypes.Store.User.read, {
                bubbles: true,
                cancelable: true,
                composed: true,
                detail: {
                    key,
                    organizationId,
                    result: undefined,
                },
            });
            target.dispatchEvent(e);
            return e.detail.result;
        },
        list: async (organizationId, options, target = document.body) => {
            const e = new CustomEvent(EventTypes.Store.User.list, {
                bubbles: true,
                cancelable: true,
                composed: true,
                detail: {
                    organizationId,
                    options,
                    result: undefined,
                },
            });
            target.dispatchEvent(e);
            return e.detail.result;
        },
    }),
});
//# sourceMappingURL=StoreEvents.js.map