import { computed } from 'vue';
import { useContentSourceStore } from '../../application/store/contentSourceStore';

export function useContentSources() {
    const store = useContentSourceStore();

    // Computed properties pour un accès facile aux données
    const sources = computed(() => store.sources);
    const currentSource = computed(() => store.currentSource);
    const isLoading = computed(() => store.loadingSources || store.loadingCurrentSource);
    const isSaving = computed(() => store.saving);
    const hasError = computed(() => store.sourcesError || store.currentSourceError || store.saveError);

    // Getters
    const getSourceById = (id) => store.getSourceById(id);
    const getSourcesByType = (type) => store.getSourcesByType(type);
    const htmlSources = computed(() => store.htmlSources);
    const javascriptSources = computed(() => store.javascriptSources);

    // Actions
    const loadSources = () => store.loadSources();
    const loadSource = (id) => store.loadSource(id);
    const createSource = (data) => store.createSource(data);
    const updateSource = (id, data) => store.updateSource(id, data);
    const exportSources = () => store.exportSources();
    const importSources = (data) => store.importSources(data);
    const clearCurrentSource = () => store.clearCurrentSource();
    const clearErrors = () => store.clearErrors();

    return {
        // Data
        sources,
        currentSource,

        // States
        isLoading,
        isSaving,
        hasError,

        // Getters
        getSourceById,
        getSourcesByType,
        htmlSources,
        javascriptSources,

        // Actions
        loadSources,
        loadSource,
        createSource,
        updateSource,
        exportSources,
        importSources,
        clearCurrentSource,
        clearErrors
    };
}
