"use strict";

const Promise = require("bluebird");
const loadImageFile = require("../load-image-file");
const elementToThumbnail = require("../element-to-thumbnail");

const { validateOptions } = require("@validatem/core");
const required = require("@validatem/required");
const isInteger = require("@validatem/is-integer");
const isString = require("@validatem/is-string");

module.exports = function thumbnailImageBrowser(_options) {
	let { file, width, height, mimetype } = validateOptions(arguments, {
		file: [ required ], // FIXME
		maximumWidth: [ required, isInteger ], // FIXME: positive
		maximumHeight: [ required, isInteger ], // FIXME: positive
		mimetype: [ required, isString ]
	});

	return Promise.try(() => {
		return loadImageFile(file);
	}).then((element) => {
		return elementToThumbnail({
			element: element,
			maximumWidth: width,
			maximumHeight: height,
			mimetype: mimetype
		});
	});
};
