import { __decorate } from "tslib";
import { Component, Prop, Vue } from "vue-property-decorator";
import Loading from "@mr-hope/vuepress-shared-utils/icons/LoadingIcon.vue";
import debounce from "lodash.debounce";
import presets from "./presets";
let svg;
let FlowChart = class FlowChart extends Vue {
    constructor() {
        super(...arguments);
        this.loading = true;
        this.scale = 1;
    }
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    get $preset() {
        const preset = presets[this.preset];
        if (!preset) {
            console.warn(`[md-enhance:flowchart] Unknown preset: ${this.preset}`);
            return presets.vue;
        }
        return preset;
    }
    get resize() {
        return debounce(() => {
            const scale = this.getScale(window.innerWidth);
            if (this.scale !== scale) {
                this.scale = scale;
                svg.drawSVG(this.id, { ...this.$preset, scale });
            }
        }, 100);
    }
    mounted() {
        const delay = () => new Promise((resolve) => setTimeout(resolve, 500));
        this.$el.setAttribute("id", this.id);
        void Promise.all([
            import(/* webpackChunkName: "flowchart" */ "flowchart.js"),
            delay(),
        ]).then(([flowchart]) => {
            const { parse } = flowchart;
            svg = parse(this.code);
            this.scale = this.getScale(window.innerWidth);
            svg.drawSVG(this.id, { ...this.$preset, scale: this.scale });
            this.loading = false;
            window.addEventListener("resize", this.resize);
        });
    }
    beforeDestory() {
        window.removeEventListener("resize", this.resize);
    }
    getScale(width) {
        return width < 419 ? 0.8 : width > 1280 ? 1 : 0.9;
    }
};
__decorate([
    Prop({ type: String, required: true })
], FlowChart.prototype, "id", void 0);
__decorate([
    Prop({ type: String, required: true })
], FlowChart.prototype, "code", void 0);
__decorate([
    Prop({ type: String, default: "vue" })
], FlowChart.prototype, "preset", void 0);
FlowChart = __decorate([
    Component({ components: { Loading } })
], FlowChart);
export default FlowChart;
