/*
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 = {
    findText: '',
    findRegexp: '',
    regexpFlags: '',
    replace: ''
};
class FindAndReplaceInSelection extends obsidian.Plugin {
    onload() {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.loadSettings();
            this.addCommand({
                id: 'find-and-replace-in-selection',
                name: 'Find and replace in selection',
                callback: () => this.findAndReplace()
            });
            this.addSettingTab(new SettingTab(this.app, this));
        });
    }
    findAndReplace() {
        let editor = this.getEditor();
        if (editor) {
            let selectedText = this.getSelectedText(editor);
            if (this.settings.findText && this.settings.findText != "") {
                selectedText = selectedText.split(this.settings.findText).join(this.settings.replace);
            }
            if (this.settings.findRegexp && this.settings.findRegexp != "") {
                var re = new RegExp(this.settings.findRegexp, this.settings.regexpFlags);
                selectedText = selectedText.replace(re, this.settings.replace);
            }
            editor.replaceSelection(selectedText);
        }
    }
    getEditor() {
        var _a;
        return (_a = this.app.workspace.getActiveViewOfType(obsidian.MarkdownView)) === null || _a === void 0 ? void 0 : _a.sourceMode.cmEditor;
    }
    getSelectedText(editor) {
        if (!editor.somethingSelected())
            this.selectLineUnderCursor(editor);
        return editor.getSelection();
    }
    selectLineUnderCursor(editor) {
        let selection = this.getLineUnderCursor(editor);
        editor.getDoc().setSelection(selection.start, selection.end);
    }
    getLineUnderCursor(editor) {
        let fromCh, toCh;
        let cursor = editor.getCursor();
        fromCh = 0;
        toCh = editor.getLine(cursor.line).length;
        return {
            start: { line: cursor.line, ch: fromCh },
            end: { line: cursor.line, ch: toCh },
        };
    }
    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);
        });
    }
}
class SettingTab extends obsidian.PluginSettingTab {
    constructor(app, plugin) {
        super(app, plugin);
        this.plugin = plugin;
    }
    display() {
        let { containerEl } = this;
        containerEl.empty();
        containerEl.createEl('h2', { text: 'Find and replace in selection - Settings' });
        new obsidian.Setting(containerEl)
            .setName('Text to find')
            .setDesc('Leave empty to ignore')
            .addText(text => text
            .setPlaceholder('')
            .setValue(this.plugin.settings.findText)
            .onChange((value) => __awaiter(this, void 0, void 0, function* () {
            this.plugin.settings.findText = value;
            yield this.plugin.saveSettings();
        })));
        new obsidian.Setting(containerEl)
            .setName('RegExp to find')
            .setDesc('Leave empty to ignore')
            .addText(text => text
            .setPlaceholder('Example: (\w+)\s(\w+)')
            .setValue(this.plugin.settings.findRegexp)
            .onChange((value) => __awaiter(this, void 0, void 0, function* () {
            this.plugin.settings.findRegexp = value;
            yield this.plugin.saveSettings();
        })));
        new obsidian.Setting(containerEl)
            .setName('RegExp flags')
            .setDesc('Type "gmi" for global, multiline, insensitive')
            .addText(text => text
            .setPlaceholder('Example: gmi')
            .setValue(this.plugin.settings.regexpFlags)
            .onChange((value) => __awaiter(this, void 0, void 0, function* () {
            this.plugin.settings.regexpFlags = value;
            yield this.plugin.saveSettings();
        })));
        new obsidian.Setting(containerEl)
            .setName('Replace by')
            .setDesc('Text to be inserted')
            .addText(text => text
            .setPlaceholder('Example: $2, $1')
            .setValue(this.plugin.settings.replace)
            .onChange((value) => __awaiter(this, void 0, void 0, function* () {
            this.plugin.settings.replace = value;
            yield this.plugin.saveSettings();
        })));
    }
}

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