import { DateTime } from "luxon";

export default function(eleventyConfig) {
	// Return the keys used in an object
	eleventyConfig.addFilter("getKeys", target => {
		return Object.keys(target);
	});

	/* For <time> elements */
	eleventyConfig.addFilter("htmlDateString", (dateObj) => {
		return DateTime.fromJSDate(dateObj, { zone: "utc" }).toFormat('yyyy-LL-dd');
	});

	/* Human-readable dates */
	eleventyConfig.addFilter("readableDate", (dateObj, format, zone) => {
		return DateTime.fromJSDate(dateObj, { zone: zone || "utc" })
			.toLocaleString(DateTime.DATE_FULL);
	});

	/* Filter out structural tags */
	eleventyConfig.addFilter("removeBasicTags", (tags) => {
		return tags.filter(tag => ["all", "posts", "gallery", "reference", "tagPagination"].indexOf(tag) === -1);
	});

	/* What it says */
	eleventyConfig.addFilter("sortAlphabetically", strings =>
		(strings || []).sort((b, a) => b.localeCompare(a))
	);

	/* Remove year from image filenames for OG metadata */
	eleventyConfig.addFilter("toOgFilename", (filename) => {
		return filename.split("/")[1];
	})

};
