"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const tempseal_1 = require("@sealcode/tempseal");
const path_1 = require("path");
const fs_1 = require("fs");
const Sharp = require("sharp");
const util_1 = require("util");
const asyncStat = util_1.promisify(fs_1.stat);
exports.loadPhotoInLogo = (add_effect, props) => __awaiter(void 0, void 0, void 0, function* () {
    const file_info = yield asyncStat(props.image_path);
    const file = new tempseal_1.SideEffects.File(path_1.basename(props.image_path) + "in-logo.svg", () => render(props), [
        props.image_path,
        file_info.mtime,
        props.direction,
        props.img_color_mode,
    ]);
    yield add_effect(file);
    return yield file.getUrlPlaceholder();
});
const extension_to_data_path = {
    ".jpg": "data:image/jpeg",
    ".jpeg": "data:image/jpeg",
    ".png": "data:image/png",
};
function render({ direction, image_path, img_color_mode, }) {
    return __awaiter(this, void 0, void 0, function* () {
        const image_extension = path_1.extname(image_path);
        const image_data_path = extension_to_data_path[image_extension];
        if (!image_data_path) {
            throw new Error(`Unknown image extension: ${image_extension}`);
        }
        return /* HTML */ `
		<svg
			xmlns:dc="http://purl.org/dc/elements/1.1/"
			xmlns:cc="http://creativecommons.org/ns#"
			xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
			xmlns:svg="http://www.w3.org/2000/svg"
			xmlns="http://www.w3.org/2000/svg"
			xmlns:xlink="http://www.w3.org/1999/xlink"
			id="svg20"
			version="1.1"
			viewBox="0 0 61.014999 47.285"
		>
			<defs id="defs24">
				<clipPath id="clipPath845" clipPathUnits="userSpaceOnUse">
					<path
						id="path847"
						d="M 23.798828,0 0,23.642578 23.798828,47.285156 30.507812,40.621094 37.216797,47.285156 61.015625,23.642578 37.216797,0 30.507812,6.6640625 Z"
						style="fill:#5294a1;fill-rule:evenodd"
					/>
				</clipPath>
			</defs>
			<image
				clip-path="url(#clipPath845)"
				width="61.138027"
				height="47.285"
				preserveAspectRatio="none"
				style="image-rendering:optimizeQuality"
				xlink:href="${image_data_path};base64,${yield getEncodedImageData(image_path, img_color_mode)}"
				id="image21"
				x="-0.059999999"
				y="0"
			/>
			<g
				transform="translate(-0.052,-0.52200015)"
				style="fill:#ffffff;fill-rule:evenodd"
				id="g4627"
			/>
			<g
				transform="translate(-0.052,-0.52200015)"
				style="fill:#ffffff;fill-rule:evenodd"
				id="g4629"
			/>
			${direction === "left"
            ? `
				<path
					id="path4"
					d="M 23.79883,47.285 47.59766,23.642422 23.79883,-1.5582259e-4 0,23.642422 Z M 30.51758,40.601406 13.44336,23.638516 30.51758,6.6775782 47.58985,23.638516 Z"
					style="opacity:0.65;fill:#5294a1;fill-rule:evenodd"
				/>
				<path
					d="m 29.201,23.587 8.352,-8.298 8.353,8.298 -8.353,8.297 z"
					id="path8"
					style="opacity:0.65;fill:#8bbac4;fill-rule:evenodd"
				/>`
            : `
				<path
				     id="path4"
				     d="M 37.21683,-1.5582259e-4 13.418,23.642422 37.21683,47.285 61.01566,23.642422 Z M 30.49808,6.6834379 47.5723,23.646328 30.49808,40.607266 13.42581,23.646328 Z"
				     style="opacity:0.65;fill:#5294a1;fill-rule:evenodd" />
				  <path
				     d="m 13.201,23.587 8.352,-8.298 8.353,8.298 -8.353,8.297 z"
				     id="path8"
				     style="opacity:0.65;fill:#8bbac4;fill-rule:evenodd" />`}
			<g
				transform="translate(-0.052,-0.522)"
				id="g12"
				style="fill:#ffffff;fill-rule:evenodd"
			/>
			<g
				transform="translate(-0.052,-0.522)"
				id="g16"
				style="fill:#ffffff;fill-rule:evenodd"
			/>
		</svg>
	`;
    });
}
const photo_size = [662, 512];
function getEncodedImageData(image_path, img_color_mode) {
    return __awaiter(this, void 0, void 0, function* () {
        const image = yield Sharp(image_path)
            .resize({
            width: photo_size[0],
            height: photo_size[1],
            fit: Sharp.fit.cover,
        })
            .grayscale(img_color_mode == "grayscale")
            .toBuffer();
        const base64_image_data = image.toString("base64");
        return base64_image_data;
    });
}
//# sourceMappingURL=photo-in-logo-loader.js.map