const getAllBlogs = async function getAllBlogs() {
    const d = new Date();
    let time = d.getTime();

    const config = useRuntimeConfig();

    const res = await $fetch(config.public.apiBase + '/blogs.json?' + time)
        .catch((error) => error.data);
    return res;
}

const getBlogSubsidiary = async function getBlogSubsidiary() {
    const d = new Date();
    let time = d.getTime();

    const config = useRuntimeConfig();

    const res = await $fetch(config.public.apiBase + '/subsidiary_list.json?' + time)
        .catch((error) => error.data);
    return res;
}

const getArchives = async function getArchives() {
    const d = new Date();
    let time = d.getTime();

    const config = useRuntimeConfig();

    const res = await $fetch(config.public.apiBase + '/blog_archive_list.json?' + time)
        .catch((error) => error.data);
    return res;
}

const getBlogDetailsByTitle = async function getBlogDetailsByTitle(title: string) {
    const d = new Date();
    let time = d.getTime();

    const config = useRuntimeConfig();

    const res = await $fetch(config.public.apiBase + '/blog-' + title +'.json?' + time)
        .catch((error) => error.data);
    return res;
}

const fetchArchiveBlogsByDate = async function fetchArchiveBlogsByDate(date: string) {
    const d = new Date();
    let time = d.getTime();

    const config = useRuntimeConfig();

    const res = await $fetch(config.public.apiBase + '/' + date +'-blogs.json?' + time)
        .catch((error) => error.data);
    return res;
}

const getSubsidiaryDetailsById = async function getSubsidiaryDetailsById(id: string) {
    const d = new Date();
    let time = d.getTime();

    const config = useRuntimeConfig();

    const res = await $fetch(config.public.apiBase + '/sub-' + id + '-blogs' +'.json?' + time)
        .catch((error) => error.data);
    return res;
}


export default {
    getAllBlogs: getAllBlogs,
    getBlogSubsidiary: getBlogSubsidiary,
    getArchives: getArchives,
    getBlogDetailsByTitle:getBlogDetailsByTitle,
    fetchArchiveBlogsByDate:fetchArchiveBlogsByDate,
    getSubsidiaryDetailsById:getSubsidiaryDetailsById,
    blogs: "blogs"
}

// const getAllBlogs = async function getAllBlogs() {
//     const d = new Date();
//     let time = d.getTime();

//     const config = useRuntimeConfig();

//     const res = await $fetch(config.public.apiBase + '/blogs.json?' + time)
//         .catch((error) => error.data);
//     return res;
// }

// const getBlogCategories = async function getBlogCategories() {
//     const d = new Date();
//     let time = d.getTime();

//     const config = useRuntimeConfig();

//     const res = await $fetch(config.public.apiBase + '/blog_category.json?' + time)
//         .catch((error) => error.data);
//     return res;
// }

// export default {
//     getAllBlogs: getAllBlogs,
//     getBlogCategories: getBlogCategories,
//     blogs: "blogs"
// }

// export default () => {
//     const config = useRuntimeConfig();
//     const API_URL: string = config.public.apiBase;

//     const get = async <T>(endpoint: string) => {
//         console.log("endpoint======", endpoint);
//         return useFetch<T>(`${API_URL}/${endpoint}`);
//     };

//     const getAllBlogs = async () => {
//         const d = new Date();
//         let time = d.getTime();
//         let query: string = `/blogs.json? + ${time}`;
//         var response = get<any>(query);
//         console.log("response======", (await response).data.value.data);
//         return get<any>(query);
//     };

//     // const getPost = async (slug: string) => {
//     //   return get<Post[]>(`posts?slug=${slug}&_embed=1`);
//     // };

//     // const getCatgories = async (fields: string = "name,slug,count") => {
//     //   return get<any>(`categories`);
//     // };

//     // const getCatgory = async (slug: string) => {
//     //   return get<any>(`categories?slug=${slug}`);
//     // };

//     return {
//         get,
//         getAllBlogs
//     };
// };