var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
import { I as autoStretchImage } from "./watermark.js";
import { E as ElementModel } from "./background.js";
const sliceKeys = ["left", "top", "right", "bottom"];
async function renderBorder(layout, ctx) {
  const { border, width, height } = layout;
  if (border && border.enable) {
    const {
      type,
      imageRepeat,
      imageSlice: slice,
      width: borderWidth,
      direction,
      opacity,
      image
    } = border;
    if (type === "image") {
      const borderUrl = image;
      const maxSliceWidth = Math.max.apply(this, sliceKeys.map((key) => slice[key]));
      const ratio = borderWidth / maxSliceWidth;
      const targetSlice = {
        left: slice.left * ratio,
        top: slice.top * ratio,
        right: slice.right * ratio,
        bottom: slice.bottom * ratio
      };
      const borderCanvas = await autoStretchImage(borderUrl, {
        targetWidth: width,
        targetHeight: height,
        imageSlice: slice,
        targetImageSlice: targetSlice,
        repeat: imageRepeat
      });
      ctx.save();
      ctx.globalAlpha = opacity;
      ctx.drawImage(borderCanvas, 0, 0);
      ctx.restore();
      return;
    } else if (type === "color") {
      ctx.save();
      ctx.globalAlpha = opacity;
      ctx.fillStyle = border.color;
      ctx.stroke();
      if (direction) {
        [
          () => ctx.fillRect(0, 0, width, borderWidth),
          () => ctx.fillRect(width - borderWidth, 0, borderWidth, height),
          () => ctx.fillRect(0, height - borderWidth, width, borderWidth),
          () => ctx.fillRect(0, 0, borderWidth, height)
        ].forEach((fn, i) => {
          direction[i] && fn();
        });
      }
      ctx.restore();
      return;
    } else if (type === "gradient") {
      return Promise.resolve();
    }
  }
  return Promise.resolve();
}
function getLayoutBackgroundImageModel(layout) {
  const { background } = layout;
  const model = new ElementModel(__spreadProps(__spreadValues({}, background.image), {
    type: "image",
    isApng: layout.isApng,
    isGif: layout.isGif
  }));
  return model;
}
export { getLayoutBackgroundImageModel as g, renderBorder as r };
