"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const vue = require("vue");
const comp = require("../../ui/src/comp.js");
const XEUtils = require("xe-utils");
require("../../ui/index.js");
const util = require("./util.js");
const vn = require("../../ui/src/vn.js");
const config = require("../../../../@vxe-ui/core/es/src/config.js");
const event = require("../../../../@vxe-ui/core/es/src/event.js");
const index_esm = require("../../../../@vxe-ui/core/es/index.esm.js");
const VxePrintComponent = comp.defineVxeComponent({
  name: "VxePrint",
  props: {
    align: {
      type: String,
      default: () => config.getConfig().print.align
    },
    title: String,
    headerAlign: {
      type: String,
      default: () => config.getConfig().print.headerAlign
    },
    footerAlign: {
      type: String,
      default: () => config.getConfig().print.footerAlign
    },
    showPageNumber: {
      type: Boolean,
      default: () => config.getConfig().print.showPageNumber
    },
    customLayout: Boolean,
    pageBreaks: Array,
    content: String,
    html: String,
    headerHtml: String,
    footerHtml: String,
    leftHtml: String,
    rightHtml: String,
    showAllPageTitle: {
      type: Boolean,
      default: () => config.getConfig().print.showAllPageTitle
    },
    pageStyle: {
      type: Object,
      default: () => config.getConfig().print.pageStyle
    },
    customStyle: {
      type: String,
      default: () => config.getConfig().print.customStyle
    },
    styleUrls: {
      type: Array,
      default: () => config.getConfig().print.styleUrls
    },
    beforeMethod: Function
  },
  emits: [],
  setup(props, context) {
    const { slots, emit } = context;
    const xID = XEUtils.uniqueId();
    const refElem = vue.ref();
    const reactData = vue.reactive({
      staticPageBreaks: []
    });
    const refMaps = {
      refElem
    };
    const computeMaps = {};
    const $xePrint = {
      xID,
      props,
      context,
      reactData,
      getRefMaps: () => refMaps,
      getComputeMaps: () => computeMaps
    };
    const dispatchEvent = (type, params, evnt) => {
      emit(type, event.createEvent(evnt, { $print: $xePrint }, params));
    };
    const printMethods = {
      dispatchEvent,
      print() {
        const { align, title, headerAlign, footerAlign, showPageNumber, customLayout, pageBreaks, content, headerHtml, footerHtml, leftHtml, rightHtml, showAllPageTitle, pageStyle, customStyle, styleUrls, beforeMethod } = props;
        const elem = refElem.value;
        return util.printHtml(Object.assign({}, {
          align,
          title,
          headerAlign,
          footerAlign,
          showPageNumber,
          customLayout,
          pageBreaks,
          content,
          headerHtml,
          footerHtml,
          leftHtml,
          rightHtml,
          showAllPageTitle,
          pageStyle,
          customStyle,
          styleUrls,
          beforeMethod
        }, {
          _pageBreaks: !!reactData.staticPageBreaks.length,
          html: util.trimHtml(elem ? elem.outerHTML : "") || props.html || props.content || ""
        }));
      }
    };
    const printPrivateMethods = {};
    Object.assign($xePrint, printMethods, printPrivateMethods);
    const renderPageConfigLayouts = () => {
      const { title, showPageNumber, showAllPageTitle, align, headerAlign, footerAlign } = props;
      const pageBreaks = props.pageBreaks || [];
      const pageCount = pageBreaks.length;
      return pageBreaks.map((item, index) => {
        const bodyHtml = item.bodyHtml;
        const headerHtml = item.headerHtml || props.headerHtml;
        const footerHtml = item.footerHtml || props.footerHtml;
        const leftHtml = item.leftHtml || props.leftHtml;
        const rightHtml = item.rightHtml || props.rightHtml;
        const currentPage = index + 1;
        const params = {
          currentPage,
          pageCount
        };
        return vue.h("div", {
          class: ["vxe-print-page-break", align ? `align--${align}` : ""]
        }, [
          vue.h("div", {
            class: ["vxe-print-page-break--header", headerAlign ? `align--${headerAlign}` : ""]
          }, headerHtml ? `${XEUtils.isFunction(headerHtml) ? headerHtml(params) : headerHtml || ""}` : [
            title && (showAllPageTitle || !index) ? vue.h("div", {
              class: "vxe-print-page-break--header-title"
            }, `${title || ""}`) : index_esm.renderEmptyElement()
          ]),
          vue.h("div", {
            class: "vxe-print-page-break--body"
          }, [
            vue.h("div", {
              class: "vxe-print-page-break--left"
            }, `${XEUtils.isFunction(leftHtml) ? leftHtml(params) : leftHtml || ""}`),
            vue.h("div", {
              class: "vxe-print-page-break--content"
            }, `${XEUtils.isFunction(bodyHtml) ? bodyHtml(params) : bodyHtml || ""}`),
            vue.h("div", {
              class: "vxe-print-page-break--right"
            }, `${XEUtils.isFunction(rightHtml) ? rightHtml(params) : rightHtml || ""}`)
          ]),
          vue.h("div", {
            class: ["vxe-print-page-break--footer", footerAlign ? `align--${footerAlign}` : ""]
          }, footerHtml ? `${XEUtils.isFunction(footerHtml) ? footerHtml(params) : footerHtml || ""}` : [
            showPageNumber ? vue.h("div", {
              class: "vxe-print-page-break--footer-page-number"
            }, `${currentPage}/${pageCount}`) : index_esm.renderEmptyElement()
          ])
        ]);
      });
    };
    const renderPageStaticLayouts = () => {
      const { title, showPageNumber, showAllPageTitle, align, headerAlign, footerAlign } = props;
      const { staticPageBreaks } = reactData;
      const pageCount = staticPageBreaks.length;
      return staticPageBreaks.map((item, index) => {
        const itemSlots = item.slots || {};
        const currentPage = index + 1;
        const defaultSlot = itemSlots.default;
        const headerSlot = itemSlots.header || slots.header;
        const footerSlot = itemSlots.footer || slots.footer;
        const leftSlot = itemSlots.left || slots.left;
        const rightSlot = itemSlots.right || slots.right;
        const params = {
          currentPage,
          pageCount
        };
        return vue.h("div", {
          class: ["vxe-print-page-break", align ? `align--${align}` : ""]
        }, [
          vue.h("div", {
            class: ["vxe-print-page-break--header", headerAlign ? `align--${headerAlign}` : ""]
          }, headerSlot ? vn.getSlotVNs(headerSlot(params)) : [
            title && (showAllPageTitle || !index) ? vue.h("div", {
              class: "vxe-print-page-break--header-title"
            }, `${title || ""}`) : index_esm.renderEmptyElement()
          ]),
          vue.h("div", {
            class: "vxe-print-page-break--body"
          }, [
            vue.h("div", {
              class: "vxe-print-page-break--left"
            }, leftSlot ? vn.getSlotVNs(leftSlot(params)) : []),
            vue.h("div", {
              class: "vxe-print-page-break--content"
            }, defaultSlot ? vn.getSlotVNs(defaultSlot(params)) : []),
            vue.h("div", {
              class: "vxe-print-page-break--right"
            }, rightSlot ? vn.getSlotVNs(rightSlot(params)) : [])
          ]),
          vue.h("div", {
            class: ["vxe-print-page-break--footer", footerAlign ? `align--${footerAlign}` : ""]
          }, footerSlot ? vn.getSlotVNs(footerSlot(params)) : [
            showPageNumber ? vue.h("div", {
              class: "vxe-print-page-break--footer-page-number"
            }, `${currentPage}/${pageCount}`) : index_esm.renderEmptyElement()
          ])
        ]);
      });
    };
    const renderVN = () => {
      const { customLayout } = props;
      const { staticPageBreaks } = reactData;
      const defaultSlot = slots.default;
      return vue.h("div", {
        ref: refElem,
        class: ["vxe-print"]
      }, customLayout ? defaultSlot ? vn.getSlotVNs(defaultSlot({})) : [] : [
        vue.h("div", {
          key: "slot",
          class: "vxe-print-slots"
        }, defaultSlot ? vn.getSlotVNs(defaultSlot({})) : [])
      ].concat(staticPageBreaks.length ? renderPageStaticLayouts() : renderPageConfigLayouts()));
    };
    $xePrint.renderVN = renderVN;
    vue.provide("$xePrint", $xePrint);
    return $xePrint;
  },
  render() {
    return this.renderVN();
  }
});
exports.default = VxePrintComponent;
