import type { AstroIntegration } from 'astro';
import { envField } from 'astro/config';
import { definePlugin } from 'studiocms/plugins';
import { readJson } from './utils/readJson.js';

function resolve(path: string) {
	return new URL(path, import.meta.url).toString();
}

// Read the package.json file for the package name and version
const { name } = readJson<{ name: string; version: string }>(resolve('../package.json'));

const identifier = 'cloudinary-js';
const servicePath = resolve('./cloudinary-js-service.js');

const CloudinaryENVIntegration = (): AstroIntegration => ({
	name: `${name}/astro-integration`,
	hooks: {
		'astro:config:setup': ({ logger, updateConfig }) => {
			logger.info('Configuring Astro ENV');

			updateConfig({
				env: {
					schema: {
						// Cloudinary Environment Variables for Custom Image Component
						CMS_CLOUDINARY_CLOUDNAME: envField.string({
							context: 'server',
							access: 'secret',
							optional: false,
						}),
					},
				},
			});
		},
	},
});

/**
 * Cloudinary Image Service
 *
 * This plugin is used to generate Cloudinary URLs for images using `@cloudinary/url-gen` for StudioCMS.
 */
function cloudinaryImageService() {
	return definePlugin({
		name: `Cloudinary JS Image Service (${identifier})`,
		identifier: name,
		hooks: {
			'studiocms:astro-config': async ({ addIntegrations }) => {
				addIntegrations(CloudinaryENVIntegration());
			},
			'studiocms:image-service': async ({ logger, setImageService }) => {
				logger.info('Initializing Cloudinary Image Service');

				setImageService({
					imageService: {
						identifier,
						servicePath,
					},
				});
			},
		},
	});
}

export default cloudinaryImageService;
