/*
THIS IS A GENERATED/BUNDLED FILE BY ROLLUP
if you want to view the source visit the plugins github repository
*/

'use strict';

var obsidian = require('obsidian');

/*! *****************************************************************************
Copyright (c) Microsoft Corporation.

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */

function __awaiter(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());
    });
}

function __classPrivateFieldGet(receiver, state, kind, f) {
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
    return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
}

function __classPrivateFieldSet(receiver, state, value, kind, f) {
    if (kind === "m") throw new TypeError("Private method is not writable");
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
    return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
}

const labeledRegex = /^[ \t]*(?:date:)(.+?)(?:^[ \t]*(?:title:)(.+?))?(?:^[ \t]*(?:content:)(.+?))?(?=^[ \t]*date:)/gimsu;
//it uses the EventFactory provided to parse the source into Events
const timelineLabeledHandler = (source, eventFactory) => {
    source += "\ndate: ";
    let sourceMatch;
    while ((sourceMatch = labeledRegex.exec(source)) !== null) {
        eventFactory.create(sourceMatch[1], sourceMatch[2], sourceMatch[3]);
    }
};
const timelineHandler = (source, eventFactory) => {
    //source to events
    const sourceSplitted = source.split(/^\s*\+ ?/gm).slice(1);
    //this is code is not readable
    //sourceSplitted.push(...new Array(3 - (sourceSplitted.length % 3)).fill(""));
    //adding 2 empty stings and 2 to sourceSplitted.length is safe and readble
    sourceSplitted.push("", "");
    const counter = sourceSplitted.length - (sourceSplitted.length % 3);
    for (let i = 0; i < counter; i += 3) {
        eventFactory.create(sourceSplitted[i], sourceSplitted[i + 1], sourceSplitted[i + 2]);
    }
};
const toExport = [
    { tag: "timeline", handler: timelineHandler },
    { tag: "timeline-labeled", handler: timelineLabeledHandler },
];

var _EventFactory_root, _EventFactory_sourcePath, _EventFactory_eventCounter;
//remove first child's top margin and last child's bottom margin
const trim = (s) => {
    if (s == null) {
        s = "";
        return;
    }
    s = s.trim();
    if (s.length === 0)
        s = "\u200B";
};
class EventFactory {
    constructor(root, sourcePath) {
        _EventFactory_root.set(this, void 0);
        _EventFactory_sourcePath.set(this, void 0);
        //#markdownRenderChild: MarkdownRenderChild;
        _EventFactory_eventCounter.set(this, void 0);
        this.create = (time, title, description) => {
            trim(time);
            trim(title);
            trim(description);
            let timeContainerEl = __classPrivateFieldGet(this, _EventFactory_root, "f").createDiv({ cls: "time-container" }), timeEl = timeContainerEl.createDiv({ cls: "time" }), infoEl = __classPrivateFieldGet(this, _EventFactory_root, "f").createDiv({ cls: "info" }), titleEl = infoEl.createDiv({ cls: "title" }), descriptionEl = infoEl.createDiv({ cls: "description" });
            obsidian.MarkdownRenderer.renderMarkdown(time, timeEl, __classPrivateFieldGet(this, _EventFactory_sourcePath, "f"), null //this.#markdownRenderChild
            );
            obsidian.MarkdownRenderer.renderMarkdown(title, titleEl, __classPrivateFieldGet(this, _EventFactory_sourcePath, "f"), null //this.#markdownRenderChild
            );
            obsidian.MarkdownRenderer.renderMarkdown(description, descriptionEl, __classPrivateFieldGet(this, _EventFactory_sourcePath, "f"), null //this.#markdownRenderChild
            );
            // regulate(descriptionEl);
            // regulate(timeEl);
            // regulate(titleEl);
            __classPrivateFieldSet(this, _EventFactory_eventCounter, +__classPrivateFieldGet(this, _EventFactory_eventCounter, "f") + 1, "f");
        };
        this.getEventCounter = () => __classPrivateFieldGet(this, _EventFactory_eventCounter, "f");
        __classPrivateFieldSet(this, _EventFactory_root, root, "f");
        __classPrivateFieldSet(this, _EventFactory_sourcePath, sourcePath, "f");
        //this.#markdownRenderChild = new MarkdownRenderChild(root);
        //this.markdownRenderChild.containerEl = root;
        __classPrivateFieldSet(this, _EventFactory_eventCounter, 0, "f");
    }
}
_EventFactory_root = new WeakMap(), _EventFactory_sourcePath = new WeakMap(), _EventFactory_eventCounter = new WeakMap();

const classRegex = /(?<=^\s*)\[.+?\]/gs;
const stringToClassArray = (input) => {
    input = input.trim();
    if (input[0] != "[" || input[input.length - 1] != "]")
        return [];
    return input
        .substring(1, input.length - 1)
        .trim()
        .split(/\s*,\s*/);
};
class MyPlugin extends obsidian.Plugin {
    constructor() {
        // addLinksToCache(
        // 	links: NodeListOf<HTMLAnchorElement>,
        // 	sourcePath: string
        // ): void {
        // 	/* //@ts-expect-error
        //     this.app.metadataCache.resolveLinks(sourcePath); */
        // 	for (let i = 0; i < links.length; i++) {
        // 		const a = links[i];
        // 		if (a.dataset.href) {
        // 			let file = this.app.metadataCache.getFirstLinkpathDest(
        // 				a.dataset.href,
        // 				""
        // 			);
        // 			let cache, path;
        // 			if (file && file instanceof TFile) {
        // 				cache = this.app.metadataCache.resolvedLinks;
        // 				path = file.path;
        // 			} else {
        // 				cache = this.app.metadataCache.unresolvedLinks;
        // 				path = a.dataset.href;
        // 			}
        // 			if (!cache[sourcePath]) {
        // 				cache[sourcePath] = {
        // 					[path]: 0,
        // 				};
        // 			}
        // 			let resolved = cache[sourcePath];
        // 			if (!resolved[path]) {
        // 				resolved[path] = 0;
        // 			}
        // 			resolved[path] += 1;
        // 			cache[sourcePath] = resolved;
        // 		}
        // 	}
        // }
        super(...arguments);
        this.onload = () => __awaiter(this, void 0, void 0, function* () {
            toExport.forEach(({ tag, handler }) => {
                this.registerMarkdownCodeBlockProcessor(tag, (source, el, ctx) => {
                    let mainLine = el.createDiv({
                        cls: "main-line",
                    });
                    el.addClass("timeline");
                    let classMatch = source.match(classRegex);
                    if (classMatch !== null) {
                        let classes = stringToClassArray(classMatch[0]);
                        el.addClasses(classes);
                    }
                    let eventFactory = new EventFactory(el, ctx.sourcePath);
                    handler(source, eventFactory);
                    mainLine.style.gridRowEnd = `${eventFactory.getEventCounter() + 1}`;
                    // const links = el.querySelectorAll<HTMLAnchorElement>("a.internal-link");
                    // this.addLinksToCache(links, ctx.sourcePath);
                });
                // this.registerEvent(
                // 	this.app.metadataCache.on("resolve", (file) => {
                // 		if (this.app.workspace.getActiveFile() != file) return;
                // 		const view = this.app.workspace.getActiveViewOfType(MarkdownView);
                // 		if (!view || !(view instanceof MarkdownView)) return;
                // 		const admonitionLinks =
                // 			view.contentEl.querySelectorAll<HTMLAnchorElement>(
                // 				"a.internal-link"
                // 			);
                // 		this.addLinksToCache(admonitionLinks, file.path);
                // 	})
                // );
            });
        });
    }
}

module.exports = MyPlugin;
//# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 33000 characters]
