"use strict";
const common_vendor = require("../../../../common/vendor.js");
const sheep_components_sShareModal_canvasPoster_poster_index = require("./poster/index.js");
async function useCanvas(options, vm) {
  const width = options.width;
  const qsc = new common_vendor.QSCanvas(
    {
      canvasId: options.canvasId,
      width: options.width,
      height: options.height,
      setCanvasWH: (canvas) => {
        options.height = canvas.height;
      }
    },
    vm
  );
  let drawer = await sheep_components_sShareModal_canvasPoster_poster_index.getPosterData(options);
  const background = await qsc.drawImg({
    type: "image",
    val: drawer.background,
    x: 0,
    y: 0,
    width,
    mode: "widthFix",
    zIndex: 0
  });
  await qsc.updateCanvasWH({
    width: background.width,
    height: background.bottom
  });
  let list = drawer.list;
  for (let i = 0; i < list.length; i++) {
    let item = list[i];
    if (item.type === "text") {
      await qsc.drawText(item);
    }
    if (item.type === "image") {
      if (item.d) {
        qsc.setCircle({
          x: item.x,
          y: item.y,
          d: item.d,
          clip: true
        });
      }
      if (item.r) {
        qsc.setRect({
          x: item.x,
          y: item.y,
          height: item.height,
          width: item.width,
          r: item.r,
          clip: true
        });
      }
      try {
        await qsc.drawImg(item);
      } catch (error) {
        console.log(error);
      }
      qsc.restore();
    }
    if (item.type === "rect") {
      if (item.r) {
        qsc.setRect({
          x: item.x,
          y: item.y,
          height: item.height,
          width: item.width,
          r: item.r,
          clip: true
        });
      }
      try {
        await qsc.draw(item);
      } catch (error) {
        console.log(error);
      }
      qsc.restore();
    }
    if (item.type === "qrcode") {
      console.log(item);
      await qsc.drawQrCode(item);
    }
  }
  await qsc.draw();
  setTimeout(async () => {
    options.src = await qsc.toImage();
  }, 100);
  return options;
}
exports.useCanvas = useCanvas;
