"use strict";

import { create } from 'zustand';
const initialState = {
  assets: {},
  uploadProgress: {},
  loading: {
    uploading: false,
    linking: false,
    deleting: false
  },
  errors: {}
};
export const useAssetStore = create((set, get) => ({
  ...initialState,
  // Asset management
  setAsset: asset => {
    set(state => ({
      assets: {
        ...state.assets,
        [asset.id]: asset
      }
    }));
  },
  setAssets: assets => {
    set(state => {
      const assetMap = assets.reduce((acc, asset) => {
        acc[asset.id] = asset;
        return acc;
      }, {});
      return {
        assets: {
          ...state.assets,
          ...assetMap
        }
      };
    });
  },
  removeAsset: assetId => {
    set(state => {
      const {
        [assetId]: removed,
        ...rest
      } = state.assets;
      return {
        assets: rest
      };
    });
  },
  // Upload progress
  setUploadProgress: (fileId, progress) => {
    set(state => ({
      uploadProgress: {
        ...state.uploadProgress,
        [fileId]: progress
      }
    }));
  },
  removeUploadProgress: fileId => {
    set(state => {
      const {
        [fileId]: removed,
        ...rest
      } = state.uploadProgress;
      return {
        uploadProgress: rest
      };
    });
  },
  // Link management
  addLink: (assetId, link) => {
    set(state => {
      const asset = state.assets[assetId];
      if (!asset) return state;

      // Check if link already exists
      const existingLink = asset.links.find(l => l.app === link.app && l.entityType === link.entityType && l.entityId === link.entityId);
      if (existingLink) return state;
      const updatedAsset = {
        ...asset,
        links: [...asset.links, link],
        usageCount: asset.links.length + 1
      };
      return {
        assets: {
          ...state.assets,
          [assetId]: updatedAsset
        }
      };
    });
  },
  removeLink: (assetId, app, entityType, entityId) => {
    set(state => {
      const asset = state.assets[assetId];
      if (!asset) return state;
      const filteredLinks = asset.links.filter(link => !(link.app === app && link.entityType === entityType && link.entityId === entityId));
      const updatedAsset = {
        ...asset,
        links: filteredLinks,
        usageCount: filteredLinks.length,
        status: filteredLinks.length === 0 ? 'trash' : asset.status
      };
      return {
        assets: {
          ...state.assets,
          [assetId]: updatedAsset
        }
      };
    });
  },
  // Loading states
  setUploading: uploading => {
    set(state => ({
      loading: {
        ...state.loading,
        uploading
      }
    }));
  },
  setLinking: linking => {
    set(state => ({
      loading: {
        ...state.loading,
        linking
      }
    }));
  },
  setDeleting: deleting => {
    set(state => ({
      loading: {
        ...state.loading,
        deleting
      }
    }));
  },
  // Error management
  setUploadError: error => {
    set(state => ({
      errors: {
        ...state.errors,
        upload: error
      }
    }));
  },
  setLinkError: error => {
    set(state => ({
      errors: {
        ...state.errors,
        link: error
      }
    }));
  },
  setDeleteError: error => {
    set(state => ({
      errors: {
        ...state.errors,
        delete: error
      }
    }));
  },
  clearErrors: () => {
    set({
      errors: {}
    });
  },
  // Utility methods
  getAssetsByApp: app => {
    const {
      assets
    } = get();
    return Object.values(assets).filter(asset => asset.links.some(link => link.app === app));
  },
  getAssetsByEntity: (app, entityType, entityId) => {
    const {
      assets
    } = get();
    return Object.values(assets).filter(asset => asset.links.some(link => link.app === app && link.entityType === entityType && link.entityId === entityId));
  },
  getAssetUsageCount: assetId => {
    const {
      assets
    } = get();
    const asset = assets[assetId];
    return asset ? asset.usageCount : 0;
  },
  isAssetLinked: (assetId, app, entityType, entityId) => {
    const {
      assets
    } = get();
    const asset = assets[assetId];
    if (!asset) return false;
    return asset.links.some(link => link.app === app && link.entityType === entityType && link.entityId === entityId);
  },
  // Reset store
  reset: () => {
    set(initialState);
  }
}));

// Selector hooks for convenience
export const useAssets = () => useAssetStore(state => Object.values(state.assets));
export const useAsset = assetId => useAssetStore(state => state.assets[assetId]);
export const useUploadProgress = () => useAssetStore(state => state.uploadProgress);
export const useAssetLoading = () => useAssetStore(state => state.loading);
export const useAssetErrors = () => useAssetStore(state => state.errors);

// Typed selectors for specific use cases
export const useAssetsByApp = app => useAssetStore(state => state.getAssetsByApp(app));
export const useAssetsByEntity = (app, entityType, entityId) => useAssetStore(state => state.getAssetsByEntity(app, entityType, entityId));
export const useAssetUsageCount = assetId => useAssetStore(state => state.getAssetUsageCount(assetId));
export const useIsAssetLinked = (assetId, app, entityType, entityId) => useAssetStore(state => state.isAssetLinked(assetId, app, entityType, entityId));
//# sourceMappingURL=assetStore.js.map