import * as plugins from './plugins.ts';
import * as appstate from './appstate.ts';
import * as interfaces from '../ts_interfaces/index.ts';
import type { IStorageDataProvider } from '@design.estate/dees-catalog';

const getIdentity = (): interfaces.data.IIdentity => {
  return appstate.loginStatePart.getState().identity!;
};

export const createDataProvider = (): IStorageDataProvider => ({
  async listObjects(bucket: string, prefix?: string, delimiter?: string) {
    const typedRequest = new plugins.domtools.plugins.typedrequest.TypedRequest<
      interfaces.requests.IReq_ListObjects
    >('/typedrequest', 'listObjects');
    const response = await typedRequest.fire({
      identity: getIdentity(),
      bucketName: bucket,
      prefix: prefix || '',
      delimiter: delimiter || '/',
    });
    return {
      objects: response.result.objects.map((obj) => ({
        key: obj.key,
        size: obj.size,
        lastModified: new Date(obj.lastModified).toISOString(),
      })),
      prefixes: response.result.commonPrefixes,
    };
  },

  async getObject(bucket: string, key: string) {
    const typedRequest = new plugins.domtools.plugins.typedrequest.TypedRequest<
      interfaces.requests.IReq_GetObject
    >('/typedrequest', 'getObject');
    return await typedRequest.fire({
      identity: getIdentity(),
      bucketName: bucket,
      key,
    });
  },

  async putObject(bucket: string, key: string, base64Content: string, contentType: string) {
    const typedRequest = new plugins.domtools.plugins.typedrequest.TypedRequest<
      interfaces.requests.IReq_PutObject
    >('/typedrequest', 'putObject');
    const response = await typedRequest.fire({
      identity: getIdentity(),
      bucketName: bucket,
      key,
      base64Content,
      contentType,
    });
    return response.ok;
  },

  async deleteObject(bucket: string, key: string) {
    const typedRequest = new plugins.domtools.plugins.typedrequest.TypedRequest<
      interfaces.requests.IReq_DeleteObject
    >('/typedrequest', 'deleteObject');
    const response = await typedRequest.fire({
      identity: getIdentity(),
      bucketName: bucket,
      key,
    });
    return response.ok;
  },

  async deletePrefix(bucket: string, prefix: string) {
    const typedRequest = new plugins.domtools.plugins.typedrequest.TypedRequest<
      interfaces.requests.IReq_DeletePrefix
    >('/typedrequest', 'deletePrefix');
    const response = await typedRequest.fire({
      identity: getIdentity(),
      bucketName: bucket,
      prefix,
    });
    return response.ok;
  },

  async getObjectUrl(bucket: string, key: string) {
    const typedRequest = new plugins.domtools.plugins.typedrequest.TypedRequest<
      interfaces.requests.IReq_GetObjectUrl
    >('/typedrequest', 'getObjectUrl');
    const response = await typedRequest.fire({
      identity: getIdentity(),
      bucketName: bucket,
      key,
    });
    return response.url;
  },

  async moveObject(bucket: string, sourceKey: string, destKey: string) {
    const typedRequest = new plugins.domtools.plugins.typedrequest.TypedRequest<
      interfaces.requests.IReq_MoveObject
    >('/typedrequest', 'moveObject');
    return await typedRequest.fire({
      identity: getIdentity(),
      bucketName: bucket,
      sourceKey,
      destKey,
    });
  },

  async movePrefix(bucket: string, sourcePrefix: string, destPrefix: string) {
    const typedRequest = new plugins.domtools.plugins.typedrequest.TypedRequest<
      interfaces.requests.IReq_MovePrefix
    >('/typedrequest', 'movePrefix');
    return await typedRequest.fire({
      identity: getIdentity(),
      bucketName: bucket,
      sourcePrefix,
      destPrefix,
    });
  },
});
