/*
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());
    });
}

const DEFAULT_SETTINGS = {};
class ObsidianRichLinksPlugin extends obsidian.Plugin {
    onload() {
        return __awaiter(this, void 0, void 0, function* () {
            console.log("loading plugin");
            yield this.loadSettings();
            this.addRibbonIcon("link", "Rich Links", () => {
                let activeLeaf = this.app.workspace.getActiveViewOfType(obsidian.MarkdownView);
                if (activeLeaf) {
                    let editor = activeLeaf.editor;
                    this.urlToIframe(editor);
                }
            });
            this.addCommand({
                id: "create-rich-links",
                name: "Create Rich Links",
                editorCheckCallback: (checking, editor) => {
                    if (!checking) {
                        this.urlToIframe(editor);
                    }
                    return true;
                },
            });
        });
    }
    onunload() {
        console.log("unloading plugin");
    }
    isUrl(text) {
        const urlRegex = new RegExp("^(http:\\/\\/www\\.|https:\\/\\/www\\.|http:\\/\\/|https:\\/\\/)?[a-z0-9]+([\\-.]{1}[a-z0-9]+)*\\.[a-z]{2,5}(:[0-9]{1,5})?(\\/.*)?$");
        return urlRegex.test(text);
    }
    urlToIframe(editor) {
        let selectedText = editor.somethingSelected()
            ? editor.getSelection()
            : false;
        if (selectedText && this.isUrl(selectedText)) {
            const url = selectedText;
            ajaxPromise({
                url: `http://iframely.server.crestify.com/iframely?url=${url}`,
            }).then((res) => {
                const data = JSON.parse(res);
                const imageLink = data.links[0].href || '';
                editor.replaceSelection(`
<div class="rich-link-card-container"><a class="rich-link-card" href="${url}" target="_blank">
	<div class="rich-link-image-container">
		<div class="rich-link-image" style="background-image: url('${imageLink}')">
	</div>
	</div>
	<div class="rich-link-card-text">
		<h1 class="rich-link-card-title">${(data.meta.title || "").replace(/\s{3,}/g, ' ').trim()}</h1>
		<p class="rich-link-card-description">
		${(data.meta.description || "").replace(/\s{3,}/g, ' ').trim()}
		</p>
		<p class="rich-link-href">
		${url}
		</p>
	</div>
</a></div>

`);
            });
        }
        else {
            new obsidian.Notice("Select a URL to convert to rich link.");
        }
    }
    loadSettings() {
        return __awaiter(this, void 0, void 0, function* () {
            this.settings = Object.assign({}, DEFAULT_SETTINGS, yield this.loadData());
        });
    }
    saveSettings() {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.saveData(this.settings);
        });
    }
}

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