Jump to content

User:Chlod/Scripts/Deputy/AttributionNoticeTemplateEditor.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/*!
 * 
 *    ATTRIBUTION NOTICE TEMPLATE EDITOR
 * 
 *    Graphically edit attribution notice templates on a page's talk page.
 * 
 *    ------------------------------------------------------------------------
 * 
 *    Copyright 2022 Chlod Aidan Alejandro
 * 
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 * 
 *        http://www.apache.org/licenses/LICENSE-2.0
 * 
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 * 
 *    ------------------------------------------------------------------------
 * 
 *    NOTE TO USERS AND DEBUGGERS: This userscript is originally written in
 *    TypeScript. The original TypeScript code is converted to raw JavaScript
 *    during the build process. To view the original source code, visit
 * 
 *        https://github.com/ChlodAlejandro/deputy
 * 
 */
// <nowiki>
(function () {
    'use strict';

    /******************************************************************************
    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.
    ***************************************************************************** */
    /* global Reflect, Promise, SuppressedError, Symbol, Iterator */


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

    typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
        var e = new Error(message);
        return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
    };

    var dist = {};

    /* eslint-disable @typescript-eslint/no-unused-vars */
    /**
     * License: MIT
     * @author Santo Pfingsten
     * @see https://github.com/Lusito/tsx-dom
     */
    Object.defineProperty(dist, "__esModule", { value: true });
    var h_1 = dist.h = void 0;
    function applyChild(element, child) {
        if (child instanceof Element)
            element.appendChild(child);
        else if (typeof child === "string" || typeof child === "number")
            element.appendChild(document.createTextNode(child.toString()));
        else
            console.warn("Unknown type to append: ", child);
    }
    function applyChildren(element, children) {
        for (const child of children) {
            if (!child && child !== 0)
                continue;
            if (Array.isArray(child))
                applyChildren(element, child);
            else
                applyChild(element, child);
        }
    }
    function transferKnownProperties(source, target) {
        for (const key of Object.keys(source)) {
            if (key in target)
                target[key] = source[key];
        }
    }
    function createElement(tag, attrs) {
        const options = (attrs === null || attrs === void 0 ? void 0 : attrs.is) ? { is: attrs.is } : undefined;
        if (attrs === null || attrs === void 0 ? void 0 : attrs.xmlns)
            return document.createElementNS(attrs.xmlns, tag, options);
        return document.createElement(tag, options);
    }
    function h(tag, attrs, ...children) {
        if (typeof tag === "function")
            return tag(Object.assign(Object.assign({}, attrs), { children }));
        const element = createElement(tag, attrs);
        if (attrs) {
            for (const name of Object.keys(attrs)) {
                // Ignore some debug props that might be added by bundlers
                if (name === "__source" || name === "__self" || name === "is" || name === "xmlns")
                    continue;
                const value = attrs[name];
                if (name.startsWith("on")) {
                    const finalName = name.replace(/Capture$/, "");
                    const useCapture = name !== finalName;
                    const eventName = finalName.toLowerCase().substring(2);
                    element.addEventListener(eventName, value, useCapture);
                }
                else if (name === "style" && typeof value !== "string") {
                    // Special handler for style with a value of type CSSStyleDeclaration
                    transferKnownProperties(value, element.style);
                }
                else if (name === "dangerouslySetInnerHTML")
                    element.innerHTML = value;
                else if (value === true)
                    element.setAttribute(name, name);
                else if (value || value === 0)
                    element.setAttribute(name, value.toString());
            }
        }
        applyChildren(element, children);
        return element;
    }
    h_1 = dist.h = h;

    /**
     * Log errors to the console.
     *
     * @param {...any} data
     */
    function error(...data) {
        console.error('[Deputy]', ...data);
    }

    /**
     * Unwraps an OOUI widget from its JQuery `$element` variable and returns it as an
     * HTML element.
     *
     * @param el The widget to unwrap.
     * @return The unwrapped widget.
     */
    function unwrapWidget (el) {
        if (el.$element == null) {
            error(el);
            throw new Error('Element is not an OOUI Element!');
        }
        return el.$element[0];
    }

    /**
     * Gets the namespace ID from a canonical (not localized) namespace name.
     *
     * @param namespace The namespace to get
     * @return The namespace ID
     */
    function nsId(namespace) {
        return mw.config.get('wgNamespaceIds')[namespace.toLowerCase().replace(/ /g, '_')];
    }

    /**
     * Works like `Object.values`.
     *
     * @param obj The object to get the values of.
     * @return The values of the given object as an array
     */
    function getObjectValues(obj) {
        return Object.keys(obj).map((key) => obj[key]);
    }

    /**
     * Transforms the `redirects` object returned by MediaWiki's `query` action into an
     * object instead of an array.
     *
     * @param redirects
     * @param normalized
     * @return Redirects as an object
     */
    function toRedirectsObject(redirects, normalized) {
        var _a;
        if (redirects == null) {
            return {};
        }
        const out = {};
        for (const redirect of redirects) {
            out[redirect.from] = redirect.to;
        }
        // Single-level redirect-normalize loop check
        for (const normal of normalized) {
            out[normal.from] = (_a = out[normal.to]) !== null && _a !== void 0 ? _a : normal.to;
        }
        return out;
    }

    /**
     * Copies text to the clipboard. Relies on the old style of clipboard copying
     * (using `document.execCommand` due to a lack of support for `navigator`-based
     * clipboard handling).
     *
     * @param text The text to copy to the clipboard.
     */
    function copyToClipboard (text) {
        const body = document.getElementsByTagName('body')[0];
        const textarea = document.createElement('textarea');
        textarea.value = text;
        textarea.style.position = 'fixed';
        textarea.style.left = '-100vw';
        textarea.style.top = '-100vh';
        body.appendChild(textarea);
        textarea.select();
        // noinspection JSDeprecatedSymbols
        document.execCommand('copy');
        body.removeChild(textarea);
    }

    /**
     * Performs {{yesno}}-based string interpretation.
     *
     * @param value The value to check
     * @param pull Depends which direction to pull unspecified values.
     * @return If `pull` is true,
     * any value that isn't explicitly a negative value will return `true`. Otherwise,
     * any value that isn't explicitly a positive value will return `false`.
     */
    function yesNo (value, pull = true) {
        if (pull) {
            return value !== false &&
                value !== 'no' &&
                value !== 'n' &&
                value !== 'false' &&
                value !== 'f' &&
                value !== 'off' &&
                +value !== 0;
        }
        else {
            return !(value !== true &&
                value !== 'yes' &&
                value !== 'y' &&
                value !== 't' &&
                value !== 'true' &&
                value !== 'on' &&
                +value !== 1);
        }
    }

    /**
     * Normalizes the title into an mw.Title object based on either a given title or
     * the current page.
     *
     * @param title The title to normalize. Default is current page.
     * @return {mw.Title} A mw.Title object. `null` if not a valid title.
     * @private
     */
    function normalizeTitle(title) {
        if (title instanceof mw.Title) {
            return title;
        }
        else if (typeof title === 'string') {
            return new mw.Title(title);
        }
        else if (title == null) {
            // Null check goes first to avoid accessing properties of `null`.
            return new mw.Title(mw.config.get('wgPageName'));
        }
        else if (title.title != null && title.namespace != null) {
            return new mw.Title(title.title, title.namespace);
        }
        else {
            return null;
        }
    }

    /**
     * Checks if two MediaWiki page titles are equal.
     *
     * @param title1
     * @param title2
     * @return `true` if `title1` and `title2` refer to the same page
     */
    function equalTitle(title1, title2) {
        return normalizeTitle(title1).getPrefixedDb() === normalizeTitle(title2).getPrefixedDb();
    }

    var version = "0.8.0";
    var gitAbbrevHash = "b1dd0f4";
    var gitBranch = "main";
    var gitDate = "Mon, 18 Nov 2024 12:45:23 +0800";
    var gitVersion = "0.8.0+gb1dd0f4";

    /**
     *
     */
    class MwApi {
        /**
         * @return A mw.Api for the current wiki.
         */
        static get action() {
            var _a;
            return (_a = this._action) !== null && _a !== void 0 ? _a : (this._action = new mw.Api({
                ajax: {
                    headers: {
                        'Api-User-Agent': `Deputy/${version} (https://w.wiki/7NWR; User:Chlod; wiki@chlod.net)`
                    }
                },
                parameters: {
                    format: 'json',
                    formatversion: 2,
                    utf8: true,
                    errorformat: 'html',
                    errorlang: mw.config.get('wgUserLanguage'),
                    errorsuselocal: true
                }
            }));
        }
        /**
         * @return A mw.Rest for the current wiki.
         */
        static get rest() {
            var _a;
            return (_a = this._rest) !== null && _a !== void 0 ? _a : (this._rest = new mw.Rest());
        }
    }
    MwApi.USER_AGENT = `Deputy/${version} (https://w.wiki/7NWR; User:Chlod; wiki@chlod.net)`;

    /**
     * Get the API error text from an API response.
     *
     * @param errorData
     * @param n Get the `n`th error. Defaults to 0 (first error).
     */
    function getApiErrorText(errorData, n = 0) {
        var _a, _b, _c, _d, _e, _f, _g;
        // errorformat=html
        return ((_b = (_a = errorData.errors) === null || _a === void 0 ? void 0 : _a[n]) === null || _b === void 0 ? void 0 : _b.html) ?
            h_1("span", { dangerouslySetInnerHTML: (_d = (_c = errorData.errors) === null || _c === void 0 ? void 0 : _c[n]) === null || _d === void 0 ? void 0 : _d.html }) :
            (
            // errorformat=plaintext/wikitext
            (_g = (_f = (_e = errorData.errors) === null || _e === void 0 ? void 0 : _e[n]) === null || _f === void 0 ? void 0 : _f.text) !== null && _g !== void 0 ? _g : 
            // errorformat=bc
            errorData.info);
    }

    let InternalRevisionDateGetButton;
    /**
     * Initializes the process element.
     */
    function initRevisionDateGetButton() {
        InternalRevisionDateGetButton = class RevisionDateGetButton extends OO.ui.ButtonWidget {
            /**
             * @param config Configuration to be passed to the element.
             */
            constructor(config) {
                super(Object.assign({
                    icon: 'download',
                    invisibleLabel: true,
                    disabled: true
                }, config));
                this.revisionInputWidget = config.revisionInputWidget;
                this.dateInputWidget = config.dateInputWidget;
                this.revisionInputWidget.on('change', this.updateButton.bind(this));
                this.dateInputWidget.on('change', this.updateButton.bind(this));
                this.on('click', this.setDateFromRevision.bind(this));
                this.updateButton();
            }
            /**
             * Update the disabled state of the button.
             */
            updateButton() {
                this.setDisabled(isNaN(+this.revisionInputWidget.getValue()) ||
                    !!this.dateInputWidget.getValue());
            }
            /**
             * Set the date from the revision ID provided in the value of
             * `this.revisionInputWidget`.
             */
            setDateFromRevision() {
                return __awaiter(this, void 0, void 0, function* () {
                    const revid = this.revisionInputWidget.getValue();
                    if (isNaN(+revid)) {
                        mw.notify(mw.msg('deputy.ante.dateAuto.invalid'), { type: 'error' });
                        this.updateButton();
                        return;
                    }
                    this
                        .setIcon('ellipsis')
                        .setDisabled(true);
                    this.dateInputWidget.setDisabled(true);
                    yield MwApi.action.get({
                        action: 'query',
                        prop: 'revisions',
                        revids: revid,
                        rvprop: 'timestamp'
                    }).then((data) => {
                        if (data.query.badrevids != null) {
                            mw.notify(mw.msg('deputy.ante.dateAuto.missing', revid), { type: 'error' });
                            this.updateButton();
                            return;
                        }
                        this.dateInputWidget.setValue(
                        // ISO-format date
                        data.query.pages[0].revisions[0].timestamp.split('T')[0]);
                        this.dateInputWidget.setDisabled(false);
                        this.setIcon('download');
                        this.updateButton();
                    }, (_error, errorData) => {
                        mw.notify(mw.msg('deputy.ante.dateAuto.failed', getApiErrorText(errorData)), { type: 'error' });
                        this.dateInputWidget.setDisabled(false);
                        this.setIcon('download');
                        this.updateButton();
                    });
                });
            }
        };
    }
    /**
     * Creates a new RevisionDateGetButton.
     *
     * @param config Configuration to be passed to the element.
     * @return A RevisionDateGetButton object
     */
    function RevisionDateGetButton (config) {
        if (!InternalRevisionDateGetButton) {
            initRevisionDateGetButton();
        }
        return new InternalRevisionDateGetButton(config);
    }

    let InternalSmartTitleInputWidget;
    /**
     * Initializes the process element.
     */
    function initSmartTitleInputWidget() {
        InternalSmartTitleInputWidget = class SmartTitleInputWidget extends mw.widgets.TitleInputWidget {
            /**
             * @param config Configuration to be passed to the element.
             */
            constructor(config) {
                super(Object.assign(config, {
                    // Force this to be true
                    allowSuggestionsWhenEmpty: true
                }));
            }
            /**
             * @inheritDoc
             */
            getRequestQuery() {
                const v = super.getRequestQuery();
                return v || normalizeTitle().getSubjectPage().getPrefixedText();
            }
            /**
             * @inheritDoc
             */
            getQueryValue() {
                const v = super.getQueryValue();
                return v || normalizeTitle().getSubjectPage().getPrefixedText();
            }
        };
    }
    /**
     * Creates a new SmartTitleInputWidget.
     *
     * @param config Configuration to be passed to the element.
     * @return A SmartTitleInputWidget object
     */
    function SmartTitleInputWidget (config) {
        if (!InternalSmartTitleInputWidget) {
            initSmartTitleInputWidget();
        }
        return new InternalSmartTitleInputWidget(config);
    }

    let InternalPageLatestRevisionGetButton;
    /**
     * Initializes the process element.
     */
    function initPageLatestRevisionGetButton() {
        InternalPageLatestRevisionGetButton = class PageLatestRevisionGetButton extends OO.ui.ButtonWidget {
            /**
             * @param config Configuration to be passed to the element.
             */
            constructor(config) {
                super(Object.assign({
                    icon: 'download',
                    invisibleLabel: true,
                    disabled: true
                }, config));
                this.titleInputWidget = config.titleInputWidget;
                this.revisionInputWidget = config.revisionInputWidget;
                this.titleInputWidget.on('change', this.updateButton.bind(this));
                this.revisionInputWidget.on('change', this.updateButton.bind(this));
                this.on('click', this.setRevisionFromPageLatestRevision.bind(this));
                this.updateButton();
            }
            /**
             * Update the disabled state of the button.
             */
            updateButton() {
                this.setDisabled(this.titleInputWidget.getValue().trim().length === 0 ||
                    this.revisionInputWidget.getValue().trim().length !== 0 ||
                    !this.titleInputWidget.isQueryValid());
            }
            /**
             * Set the revision ID from the page provided in the value of
             * `this.titleInputWidget`.
             */
            setRevisionFromPageLatestRevision() {
                return __awaiter(this, void 0, void 0, function* () {
                    this
                        .setIcon('ellipsis')
                        .setDisabled(true);
                    this.revisionInputWidget.setDisabled(true);
                    const title = this.titleInputWidget.getValue();
                    yield MwApi.action.get({
                        action: 'query',
                        prop: 'revisions',
                        titles: title,
                        rvprop: 'ids'
                    }).then((data) => {
                        if (data.query.pages[0].missing) {
                            mw.notify(mw.msg('deputy.ante.revisionAuto.missing', title), { type: 'error' });
                            this.updateButton();
                            return;
                        }
                        this.revisionInputWidget.setValue(data.query.pages[0].revisions[0].revid);
                        this.revisionInputWidget.setDisabled(false);
                        this.setIcon('download');
                        this.updateButton();
                    }, (_error, errorData) => {
                        mw.notify(mw.msg('deputy.ante.revisionAuto.failed', getApiErrorText(errorData)), { type: 'error' });
                        this.revisionInputWidget.setDisabled(false);
                        this.setIcon('download');
                        this.updateButton();
                    });
                });
            }
        };
    }
    /**
     * Creates a new PageLatestRevisionGetButton.
     *
     * @param config Configuration to be passed to the element.
     * @return A PageLatestRevisionGetButton object
     */
    function PageLatestRevisionGetButton (config) {
        if (!InternalPageLatestRevisionGetButton) {
            initPageLatestRevisionGetButton();
        }
        return new InternalPageLatestRevisionGetButton(config);
    }

    /**
     * Shows a confirmation dialog, if the user does not have danger mode enabled.
     * If the user has danger mode enabled, this immediately resolves to true, letting
     * the action run immediately.
     *
     * Do not use this with any action that can potentially break templates, user data,
     * or cause irreversible data loss.
     *
     * @param config The user's configuration
     * @param message See {@link OO.ui.MessageDialog}'s parameters.
     * @param options See {@link OO.ui.MessageDialog}'s parameters.
     * @return Promise resolving to a true/false boolean.
     */
    function dangerModeConfirm(config, message, options) {
        if (config.all.core.dangerMode.get()) {
            return $.Deferred().resolve(true);
        }
        else {
            return OO.ui.confirm(message, options);
        }
    }

    /**
     * Log warnings to the console.
     *
     * @param {...any} data
     */
    function warn(...data) {
        console.warn('[Deputy]', ...data);
    }

    /**
     * What it says on the tin. Attempt to parse out a `title`, `diff`,
     * or `oldid` from a URL. This is useful for converting diff URLs into actual
     * diff information, and especially useful for {{copied}} templates.
     *
     * If diff parameters were not found (no `diff` or `oldid`), they will be `null`.
     *
     * @param url The URL to parse
     * @return Parsed info: `diff` or `oldid` revision IDs, and/or the page title.
     */
    function parseDiffUrl(url) {
        if (typeof url === 'string') {
            url = new URL(url);
        }
        // Attempt to get values from URL parameters (when using `/w/index.php?action=diff`)
        let oldid = url.searchParams.get('oldid');
        let diff = url.searchParams.get('diff');
        let title = url.searchParams.get('title');
        // Attempt to get information from this URL.
        tryConvert: {
            if (title && oldid && diff) {
                // Skip if there's nothing else we need to get.
                break tryConvert;
            }
            // Attempt to get values from Special:Diff short-link
            const diffSpecialPageCheck = 
            // eslint-disable-next-line security/detect-unsafe-regex
            /\/wiki\/Special:Diff\/(prev|next|\d+)(?:\/(prev|next|\d+))?/i.exec(url.pathname);
            if (diffSpecialPageCheck != null) {
                if (diffSpecialPageCheck[1] != null &&
                    diffSpecialPageCheck[2] == null) {
                    // Special:Diff/diff
                    diff = diffSpecialPageCheck[1];
                }
                else if (diffSpecialPageCheck[1] != null &&
                    diffSpecialPageCheck[2] != null) {
                    // Special:Diff/oldid/diff
                    oldid = diffSpecialPageCheck[1];
                    diff = diffSpecialPageCheck[2];
                }
                break tryConvert;
            }
            // Attempt to get values from Special:PermanentLink short-link
            const permanentLinkCheck = /\/wiki\/Special:Perma(nent)?link\/(\d+)/i.exec(url.pathname);
            if (permanentLinkCheck != null) {
                oldid = permanentLinkCheck[2];
                break tryConvert;
            }
            // Attempt to get values from article path with ?oldid or ?diff
            // eslint-disable-next-line security/detect-non-literal-regexp
            const articlePathRegex = new RegExp(mw.util.getUrl('(.*)'))
                .exec(url.pathname);
            if (articlePathRegex != null) {
                title = decodeURIComponent(articlePathRegex[1]);
                break tryConvert;
            }
        }
        // Convert numbers to numbers
        if (oldid != null && !isNaN(+oldid)) {
            oldid = +oldid;
        }
        if (diff != null && !isNaN(+diff)) {
            diff = +diff;
        }
        // Try to convert a page title
        try {
            title = new mw.Title(title).getPrefixedText();
        }
        catch (e) {
            warn('Failed to normalize page title during diff URL conversion.');
        }
        return {
            diff: diff,
            oldid: oldid,
            title: title
        };
    }

    let InternalCopiedTemplateRowPage;
    /**
     * The UI representation of a {{copied}} template row. This refers to a set of `diff`, `to`,
     * or `from` parameters on each {{copied}} template.
     *
     * Note that "Page" in the class title does not refer to a MediaWiki page, but rather
     * a OOUI PageLayout.
     */
    function initCopiedTemplateRowPage() {
        InternalCopiedTemplateRowPage = class CopiedTemplateRowPage extends OO.ui.PageLayout {
            /**
             * @param config Configuration to be passed to the element.
             */
            constructor(config) {
                const { copiedTemplateRow, parent } = config;
                if (parent == null) {
                    throw new Error('Parent dialog (CopiedTemplateEditorDialog) is required');
                }
                else if (copiedTemplateRow == null) {
                    throw new Error('Reference row (CopiedTemplateRow) is required');
                }
                const finalConfig = {
                    classes: ['cte-page-row']
                };
                super(copiedTemplateRow.id, finalConfig);
                this.parent = parent;
                this.copiedTemplateRow = copiedTemplateRow;
                this.refreshLabel();
                this.copiedTemplateRow.parent.addEventListener('destroy', () => {
                    parent.rebuildPages();
                });
                this.copiedTemplateRow.parent.addEventListener('rowDelete', () => {
                    parent.rebuildPages();
                });
                this.$element.append(this.render().$element);
            }
            /**
             * Refreshes the page's label
             */
            refreshLabel() {
                if (this.copiedTemplateRow.from && equalTitle(this.copiedTemplateRow.from, normalizeTitle(this.copiedTemplateRow.parent.parsoid.getPage())
                    .getSubjectPage())) {
                    this.label = mw.message('deputy.ante.copied.entry.shortTo', this.copiedTemplateRow.to || '???').text();
                }
                else if (this.copiedTemplateRow.to && equalTitle(this.copiedTemplateRow.to, normalizeTitle(this.copiedTemplateRow.parent.parsoid.getPage())
                    .getSubjectPage())) {
                    this.label = mw.message('deputy.ante.copied.entry.shortFrom', this.copiedTemplateRow.from || '???').text();
                }
                else {
                    this.label = mw.message('deputy.ante.copied.entry.short', this.copiedTemplateRow.from || '???', this.copiedTemplateRow.to || '???').text();
                }
                if (this.outlineItem) {
                    this.outlineItem.setLabel(this.label);
                }
            }
            /**
             * Renders this page. Returns a FieldsetLayout OOUI widget.
             *
             * @return An OOUI FieldsetLayout
             */
            render() {
                this.layout = new OO.ui.FieldsetLayout({
                    icon: 'parameter',
                    label: mw.msg('deputy.ante.copied.entry.label'),
                    classes: ['cte-fieldset']
                });
                this.layout.$element.append(this.renderButtons());
                this.layout.addItems(this.renderFields());
                return this.layout;
            }
            /**
             * Renders a set of buttons used to modify a specific {{copied}} template row.
             *
             * @return An array of OOUI FieldLayouts
             */
            renderButtons() {
                const deleteButton = new OO.ui.ButtonWidget({
                    icon: 'trash',
                    title: mw.msg('deputy.ante.copied.entry.remove'),
                    framed: false,
                    flags: ['destructive']
                });
                deleteButton.on('click', () => {
                    this.copiedTemplateRow.parent.deleteRow(this.copiedTemplateRow);
                });
                const copyButton = new OO.ui.ButtonWidget({
                    icon: 'quotes',
                    title: mw.msg('deputy.ante.copied.entry.copy'),
                    framed: false
                });
                copyButton.on('click', () => {
                    // TODO: Find out a way to l10n-ize this.
                    let attributionString = `[[WP:PATT|Attribution]]: Content ${this.copiedTemplateRow.merge ? 'merged' : 'partially copied'}`;
                    let lacking = false;
                    if (this.copiedTemplateRow.from != null &&
                        this.copiedTemplateRow.from.length !== 0) {
                        attributionString += ` from [[${this.copiedTemplateRow.from}]]`;
                    }
                    else {
                        lacking = true;
                        if (this.copiedTemplateRow.from_oldid != null) {
                            attributionString += ' from a page';
                        }
                    }
                    if (this.copiedTemplateRow.from_oldid != null) {
                        attributionString += ` as of revision [[Special:Diff/${this.copiedTemplateRow.from_oldid}|${this.copiedTemplateRow.from_oldid}]]`;
                    }
                    if (this.copiedTemplateRow.to_diff != null ||
                        this.copiedTemplateRow.to_oldid != null) {
                        // Shifting will ensure that `to_oldid` will be used if `to_diff` is missing.
                        const diffPart1 = this.copiedTemplateRow.to_oldid ||
                            this.copiedTemplateRow.to_diff;
                        const diffPart2 = this.copiedTemplateRow.to_diff ||
                            this.copiedTemplateRow.to_oldid;
                        attributionString += ` with [[Special:Diff/${diffPart1 === diffPart2 ? diffPart1 : `${diffPart1}/${diffPart2}`}|this edit]]`;
                    }
                    if (this.copiedTemplateRow.from != null &&
                        this.copiedTemplateRow.from.length !== 0) {
                        attributionString += `; refer to that page's [[Special:PageHistory/${this.copiedTemplateRow.from}|edit history]] for additional attribution`;
                    }
                    attributionString += '.';
                    copyToClipboard(attributionString);
                    if (lacking) {
                        mw.notify(mw.msg('deputy.ante.copied.entry.copy.lacking'), { title: mw.msg('deputy.ante'), type: 'warn' });
                    }
                    else {
                        mw.notify(mw.msg('deputy.ante.copied.entry.copy.success'), { title: mw.msg('deputy.ante') });
                    }
                });
                return h_1("div", { style: {
                        float: 'right',
                        position: 'absolute',
                        top: '0.5em',
                        right: '0.5em'
                    } },
                    unwrapWidget(copyButton),
                    unwrapWidget(deleteButton));
            }
            /**
             * Renders a set of OOUI InputWidgets and FieldLayouts, eventually returning an
             * array of each FieldLayout to append to the FieldsetLayout.
             *
             * @return An array of OOUI FieldLayouts
             */
            renderFields() {
                const copiedTemplateRow = this.copiedTemplateRow;
                const parsedDate = (copiedTemplateRow.date == null || copiedTemplateRow.date.trim().length === 0) ?
                    undefined : (!isNaN(new Date(copiedTemplateRow.date.trim() + ' UTC').getTime()) ?
                    (new Date(copiedTemplateRow.date.trim() + ' UTC')) : (!isNaN(new Date(copiedTemplateRow.date.trim()).getTime()) ?
                    new Date(copiedTemplateRow.date.trim()) : null));
                this.inputs = {
                    from: SmartTitleInputWidget({
                        $overlay: this.parent.$overlay,
                        placeholder: mw.msg('deputy.ante.copied.from.placeholder'),
                        value: copiedTemplateRow.from,
                        validate: /^.+$/g
                    }),
                    from_oldid: new OO.ui.TextInputWidget({
                        placeholder: mw.msg('deputy.ante.copied.from_oldid.placeholder'),
                        value: copiedTemplateRow.from_oldid,
                        validate: /^\d*$/
                    }),
                    to: SmartTitleInputWidget({
                        $overlay: this.parent.$overlay,
                        placeholder: mw.msg('deputy.ante.copied.to.placeholder'),
                        value: copiedTemplateRow.to
                    }),
                    to_diff: new OO.ui.TextInputWidget({
                        placeholder: mw.msg('deputy.ante.copied.to_diff.placeholder'),
                        value: copiedTemplateRow.to_diff,
                        validate: /^\d*$/
                    }),
                    // Advanced options
                    to_oldid: new OO.ui.TextInputWidget({
                        placeholder: mw.msg('deputy.ante.copied.to_oldid.placeholder'),
                        value: copiedTemplateRow.to_oldid,
                        validate: /^\d*$/
                    }),
                    diff: new OO.ui.TextInputWidget({
                        placeholder: mw.msg('deputy.ante.copied.diff.placeholder'),
                        value: copiedTemplateRow.diff
                    }),
                    merge: new OO.ui.CheckboxInputWidget({
                        selected: yesNo(copiedTemplateRow.merge)
                    }),
                    afd: new OO.ui.TextInputWidget({
                        placeholder: mw.msg('deputy.ante.copied.afd.placeholder'),
                        value: copiedTemplateRow.afd,
                        disabled: copiedTemplateRow.merge === undefined,
                        // Prevent people from adding the WP:AFD prefix.
                        validate: /^((?!W(iki)?p(edia)?:(A(rticles)?[ _]?f(or)?[ _]?d(eletion)?\/)).+|$)/gi
                    }),
                    date: new mw.widgets.DateInputWidget({
                        $overlay: this.parent.$overlay,
                        icon: 'calendar',
                        value: parsedDate ? `${parsedDate.getUTCFullYear()}-${parsedDate.getUTCMonth() + 1}-${parsedDate.getUTCDate()}` : undefined,
                        placeholder: mw.msg('deputy.ante.copied.date.placeholder'),
                        calendar: {
                            verticalPosition: 'above'
                        }
                    }),
                    toggle: new OO.ui.ToggleSwitchWidget()
                };
                const diffConvert = new OO.ui.ButtonWidget({
                    label: mw.msg('deputy.ante.copied.convert')
                });
                const dateAuto = RevisionDateGetButton({
                    label: mw.msg('deputy.ante.dateAuto', 'to_diff'),
                    revisionInputWidget: this.inputs.to_diff,
                    dateInputWidget: this.inputs.date
                });
                const revisionAutoFrom = PageLatestRevisionGetButton({
                    invisibleLabel: false,
                    label: mw.msg('deputy.ante.revisionAuto'),
                    title: mw.msg('deputy.ante.revisionAuto.title', 'from'),
                    titleInputWidget: this.inputs.from,
                    revisionInputWidget: this.inputs.from_oldid
                });
                const revisionAutoTo = PageLatestRevisionGetButton({
                    invisibleLabel: false,
                    label: mw.msg('deputy.ante.revisionAuto'),
                    title: mw.msg('deputy.ante.revisionAuto.title', 'to'),
                    titleInputWidget: this.inputs.to,
                    revisionInputWidget: this.inputs.to_diff
                });
                this.fieldLayouts = {
                    from: new OO.ui.FieldLayout(this.inputs.from, {
                        $overlay: this.parent.$overlay,
                        label: mw.msg('deputy.ante.copied.from.label'),
                        align: 'top',
                        help: mw.msg('deputy.ante.copied.from.help')
                    }),
                    from_oldid: new OO.ui.ActionFieldLayout(this.inputs.from_oldid, revisionAutoFrom, {
                        $overlay: this.parent.$overlay,
                        label: mw.msg('deputy.ante.copied.from_oldid.label'),
                        align: 'left',
                        help: mw.msg('deputy.ante.copied.from_oldid.help')
                    }),
                    to: new OO.ui.FieldLayout(this.inputs.to, {
                        $overlay: this.parent.$overlay,
                        label: mw.msg('deputy.ante.copied.to.label'),
                        align: 'top',
                        help: mw.msg('deputy.ante.copied.to.help')
                    }),
                    to_diff: new OO.ui.ActionFieldLayout(this.inputs.to_diff, revisionAutoTo, {
                        $overlay: this.parent.$overlay,
                        label: mw.msg('deputy.ante.copied.to_diff.label'),
                        align: 'left',
                        help: mw.msg('deputy.ante.copied.to_diff.help')
                    }),
                    // Advanced options
                    to_oldid: new OO.ui.FieldLayout(this.inputs.to_oldid, {
                        $overlay: this.parent.$overlay,
                        label: mw.msg('deputy.ante.copied.to_oldid.label'),
                        align: 'left',
                        help: mw.msg('deputy.ante.copied.to_oldid.help')
                    }),
                    diff: new OO.ui.ActionFieldLayout(this.inputs.diff, diffConvert, {
                        $overlay: this.parent.$overlay,
                        label: mw.msg('deputy.ante.copied.diff.label'),
                        align: 'inline',
                        help: new OO.ui.HtmlSnippet(mw.message('deputy.ante.copied.diff.help').plain())
                    }),
                    merge: new OO.ui.FieldLayout(this.inputs.merge, {
                        $overlay: this.parent.$overlay,
                        label: mw.msg('deputy.ante.copied.merge.label'),
                        align: 'inline',
                        help: mw.msg('deputy.ante.copied.merge.help')
                    }),
                    afd: new OO.ui.FieldLayout(this.inputs.afd, {
                        $overlay: this.parent.$overlay,
                        label: mw.msg('deputy.ante.copied.afd.label'),
                        align: 'left',
                        help: mw.msg('deputy.ante.copied.afd.help')
                    }),
                    date: new OO.ui.ActionFieldLayout(this.inputs.date, dateAuto, {
                        align: 'inline',
                        classes: ['cte-fieldset-date']
                    }),
                    toggle: new OO.ui.FieldLayout(this.inputs.toggle, {
                        label: mw.msg('deputy.ante.copied.advanced'),
                        align: 'inline',
                        classes: ['cte-fieldset-advswitch']
                    })
                };
                if (parsedDate === null) {
                    this.fieldLayouts.date.setWarnings([
                        mw.msg('deputy.ante.copied.dateInvalid', copiedTemplateRow.date)
                    ]);
                }
                // Define options that get hidden when advanced options are toggled
                const advancedOptions = [
                    this.fieldLayouts.to_oldid,
                    this.fieldLayouts.diff,
                    this.fieldLayouts.merge,
                    this.fieldLayouts.afd
                ];
                // Self-imposed deprecation notice in order to steer away from plain URL
                // diff links. This will, in the long term, make it easier to parse out
                // and edit {{copied}} templates.
                const diffDeprecatedNotice = new OO.ui.HtmlSnippet(mw.message('deputy.ante.copied.diffDeprecate').plain());
                // Hide advanced options
                advancedOptions.forEach((e) => {
                    e.toggle(false);
                });
                // ...except for `diff` if it's supplied (legacy reasons)
                if (copiedTemplateRow.diff) {
                    this.fieldLayouts.diff.toggle(true);
                    this.fieldLayouts.diff.setWarnings([diffDeprecatedNotice]);
                }
                else {
                    diffConvert.setDisabled(true);
                }
                // Attach event listeners
                this.inputs.diff.on('change', () => {
                    if (this.inputs.diff.getValue().length > 0) {
                        try {
                            // Check if the diff URL is from this wiki.
                            if (new URL(this.inputs.diff.getValue(), window.location.href).host === window.location.host) {
                                // Prefer `to_oldid` and `to_diff`
                                this.fieldLayouts.diff.setWarnings([diffDeprecatedNotice]);
                                diffConvert.setDisabled(false);
                            }
                            else {
                                this.fieldLayouts.diff.setWarnings([]);
                                diffConvert.setDisabled(true);
                            }
                        }
                        catch (e) {
                            // Clear warnings just to be safe.
                            this.fieldLayouts.diff.setWarnings([]);
                            diffConvert.setDisabled(true);
                        }
                    }
                    else {
                        this.fieldLayouts.diff.setWarnings([]);
                        diffConvert.setDisabled(true);
                    }
                });
                this.inputs.merge.on('change', (value) => {
                    this.inputs.afd.setDisabled(!value);
                });
                this.inputs.toggle.on('change', (value) => {
                    advancedOptions.forEach((e) => {
                        e.toggle(value);
                    });
                    this.fieldLayouts.to_diff.setLabel(value ? 'Ending revision ID' : 'Revision ID');
                });
                this.inputs.from.on('change', () => {
                    this.refreshLabel();
                });
                this.inputs.to.on('change', () => {
                    this.refreshLabel();
                });
                for (const _field in this.inputs) {
                    if (_field === 'toggle') {
                        continue;
                    }
                    const field = _field;
                    const input = this.inputs[field];
                    // Attach the change listener
                    input.on('change', (value) => {
                        if (input instanceof OO.ui.CheckboxInputWidget) {
                            // Specific to `merge`. Watch out before adding more checkboxes.
                            this.copiedTemplateRow[field] = value ? 'yes' : '';
                        }
                        else if (input instanceof mw.widgets.DateInputWidget) {
                            this.copiedTemplateRow[field] = value ?
                                window.moment(value, 'YYYY-MM-DD')
                                    .locale(mw.config.get('wgContentLanguage'))
                                    .format('D MMMM Y') : undefined;
                            if (value.length > 0) {
                                this.fieldLayouts[field].setWarnings([]);
                            }
                        }
                        else {
                            this.copiedTemplateRow[field] = value;
                        }
                        copiedTemplateRow.parent.save();
                        this.refreshLabel();
                    });
                    if (input instanceof OO.ui.TextInputWidget) {
                        // Rechecks the validity of the field.
                        input.setValidityFlag();
                    }
                }
                // Diff convert click handler
                diffConvert.on('click', this.convertDeprecatedDiff.bind(this));
                return getObjectValues(this.fieldLayouts);
            }
            /**
             * Converts a raw diff URL on the same wiki as the current to use `to` and `to_oldid`
             * (and `to_diff`, if available).
             */
            convertDeprecatedDiff() {
                return __awaiter(this, void 0, void 0, function* () {
                    const value = this.inputs.diff.getValue();
                    try {
                        const url = new URL(value, window.location.href);
                        if (!value) {
                            return;
                        }
                        if (url.host !== window.location.host) {
                            if (!(yield OO.ui.confirm(mw.msg('deputy.ante.copied.diffDeprecate.warnHost')))) {
                                return;
                            }
                        }
                        // From the same wiki, accept deprecation immediately.
                        // Parse out info from this diff URL
                        const parseInfo = parseDiffUrl(url);
                        let { diff, oldid } = parseInfo;
                        const { title } = parseInfo;
                        // If only an oldid was provided, and no diff
                        if (oldid && !diff) {
                            diff = oldid;
                            oldid = undefined;
                        }
                        const confirmProcess = new OO.ui.Process();
                        // Looping over the row name and the value that will replace it.
                        for (const [_rowName, newValue] of [
                            ['to_oldid', oldid],
                            ['to_diff', diff],
                            ['to', title]
                        ]) {
                            const rowName = _rowName;
                            if (newValue == null) {
                                continue;
                            }
                            if (
                            // Field has an existing value
                            this.copiedTemplateRow[rowName] != null &&
                                this.copiedTemplateRow[rowName].length > 0 &&
                                this.copiedTemplateRow[rowName] !== newValue) {
                                confirmProcess.next(() => __awaiter(this, void 0, void 0, function* () {
                                    const confirmPromise = dangerModeConfirm(window.CopiedTemplateEditor.config, mw.message('deputy.ante.copied.diffDeprecate.replace', rowName, this.copiedTemplateRow[rowName], newValue).text());
                                    confirmPromise.done((confirmed) => {
                                        if (confirmed) {
                                            this.inputs[rowName].setValue(newValue);
                                            this.fieldLayouts[rowName].toggle(true);
                                        }
                                    });
                                    return confirmPromise;
                                }));
                            }
                            else {
                                this.inputs[rowName].setValue(newValue);
                                this.fieldLayouts[rowName].toggle(true);
                            }
                        }
                        confirmProcess.next(() => {
                            this.copiedTemplateRow.parent.save();
                            this.inputs.diff.setValue('');
                            if (!this.inputs.toggle.getValue()) {
                                this.fieldLayouts.diff.toggle(false);
                            }
                        });
                        confirmProcess.execute();
                    }
                    catch (e) {
                        error('Cannot convert `diff` parameter to URL.', e);
                        OO.ui.alert(mw.msg('deputy.ante.copied.diffDeprecate.failed'));
                    }
                });
            }
            /**
             * Sets up the outline item of this page. Used in the BookletLayout.
             */
            setupOutlineItem() {
                if (this.outlineItem !== undefined) {
                    this.outlineItem
                        .setMovable(true)
                        .setRemovable(true)
                        .setIcon('parameter')
                        .setLevel(1)
                        .setLabel(this.label);
                }
            }
        };
    }
    /**
     * Creates a new CopiedTemplateRowPage.
     *
     * @param config Configuration to be passed to the element.
     * @return A CopiedTemplateRowPage object
     */
    function CopiedTemplateRowPage (config) {
        if (!InternalCopiedTemplateRowPage) {
            initCopiedTemplateRowPage();
        }
        return new InternalCopiedTemplateRowPage(config);
    }

    /**
     * An attribution notice's row or entry.
     */
    class AttributionNoticeRow {
        /**
         * @return The parent of this attribution notice row.
         */
        get parent() {
            return this._parent;
        }
        /**
         * Sets the parent. Automatically moves this template from one
         * parent's row set to another.
         *
         * @param newParent The new parent.
         */
        set parent(newParent) {
            this._parent.deleteRow(this);
            newParent.addRow(this);
            this._parent = newParent;
        }
        /**
         *
         * @param parent
         */
        constructor(parent) {
            this._parent = parent;
            const r = window.btoa((Math.random() * 10000).toString()).slice(0, 6);
            this.name = this.parent.name + '#' + r;
            this.id = window.btoa(parent.node.getTarget().wt) + '-' + this.name;
        }
        /**
         * Clones this row.
         *
         * @param parent The parent of this new row.
         * @return The cloned row
         */
        clone(parent) {
            // Odd constructor usage here allows cloning from subclasses without having
            // to re-implement the cloning function.
            // noinspection JSCheckFunctionSignatures
            return new this.constructor(this, parent);
        }
    }

    const copiedTemplateRowParameters = [
        'from', 'from_oldid', 'to', 'to_diff',
        'to_oldid', 'diff', 'date', 'afd', 'merge'
    ];
    /**
     * Represents a row/entry in a {{copied}} template.
     */
    class CopiedTemplateRow extends AttributionNoticeRow {
        // noinspection JSDeprecatedSymbols
        /**
         * Creates a new RawCopiedTemplateRow
         *
         * @param rowObjects
         * @param parent
         */
        constructor(rowObjects, parent) {
            super(parent);
            this.from = rowObjects.from;
            // eslint-disable-next-line camelcase
            this.from_oldid = rowObjects.from_oldid;
            this.to = rowObjects.to;
            // eslint-disable-next-line camelcase
            this.to_diff = rowObjects.to_diff;
            // eslint-disable-next-line camelcase
            this.to_oldid = rowObjects.to_oldid;
            this.diff = rowObjects.diff;
            this.date = rowObjects.date;
            this.afd = rowObjects.afd;
            this.merge = rowObjects.merge;
        }
        /**
         * @inheritDoc
         */
        clone(parent) {
            return super.clone(parent);
        }
        /**
         * @inheritDoc
         */
        generatePage(dialog) {
            return CopiedTemplateRowPage({
                copiedTemplateRow: this,
                parent: dialog
            });
        }
    }

    /**
     * Merges templates together. Its own class to avoid circular dependencies.
     */
    class TemplateMerger {
        /**
         * Merge an array of CopiedTemplates into one big CopiedTemplate. Other templates
         * will be destroyed.
         *
         * @param templateList The list of templates to merge
         * @param pivot The template to merge into. If not supplied, the first template
         *              in the list will be used.
         */
        static merge(templateList, pivot) {
            pivot = pivot !== null && pivot !== void 0 ? pivot : templateList[0];
            while (templateList.length > 0) {
                const template = templateList[0];
                if (template !== pivot) {
                    if (template.node.getTarget().href !== pivot.node.getTarget().href) {
                        throw new Error("Attempted to merge incompatible templates.");
                    }
                    pivot.merge(template, { delete: true });
                }
                // Pop the pivot template out of the list.
                templateList.shift();
            }
        }
    }

    /**
     * Renders the panel used to merge multiple {{split article}} templates.
     *
     * @param type
     * @param parentTemplate
     * @param mergeButton
     * @return A <div> element
     */
    function renderMergePanel(type, parentTemplate, mergeButton) {
        const mergePanel = new OO.ui.FieldsetLayout({
            classes: ['cte-merge-panel'],
            icon: 'tableMergeCells',
            label: mw.msg('deputy.ante.merge.title')
        });
        unwrapWidget(mergePanel).style.padding = '16px';
        unwrapWidget(mergePanel).style.zIndex = '20';
        // Hide by default
        mergePanel.toggle(false);
        // <select> and button for merging templates
        const mergeTarget = new OO.ui.DropdownInputWidget({
            $overlay: true,
            title: mw.msg('deputy.ante.merge.from.select')
        });
        const mergeTargetButton = new OO.ui.ButtonWidget({
            label: mw.msg('deputy.ante.merge.button')
        });
        mergeTargetButton.on('click', () => {
            const template = parentTemplate.parsoid.findNoticeType(type).find((v) => v.name === mergeTarget.getValue());
            if (template) {
                // If template found, merge and reset panel
                parentTemplate.merge(template, { delete: true });
                mergeTarget.setValue(null);
                mergePanel.toggle(false);
            }
        });
        const mergeFieldLayout = new OO.ui.ActionFieldLayout(mergeTarget, mergeTargetButton, {
            label: mw.msg('deputy.ante.merge.from.label'),
            align: 'left'
        });
        mergeButton.on('click', () => {
            mergePanel.toggle();
        });
        const mergeAllButton = new OO.ui.ButtonWidget({
            label: mw.msg('deputy.ante.merge.all'),
            flags: ['progressive']
        });
        mergeAllButton.on('click', () => {
            const notices = parentTemplate.parsoid.findNoticeType(type);
            // Confirm before merging.
            dangerModeConfirm(window.CopiedTemplateEditor.config, mw.message('deputy.ante.merge.all.confirm', `${notices.length - 1}`).text()).done((confirmed) => {
                if (confirmed) {
                    // Recursively merge all templates
                    TemplateMerger.merge(notices, parentTemplate);
                    mergeTarget.setValue(null);
                    mergePanel.toggle(false);
                }
            });
        });
        const recalculateOptions = () => {
            const notices = parentTemplate.parsoid.findNoticeType(type);
            const options = [];
            for (const notice of notices) {
                if (notice === parentTemplate) {
                    continue;
                }
                options.push({
                    data: notice.name,
                    // Messages used here:
                    // * deputy.ante.copied.label
                    // * deputy.ante.splitArticle.label
                    // * deputy.ante.mergedFrom.label
                    // * deputy.ante.mergedTo.label
                    // * deputy.ante.backwardsCopy.label
                    // * deputy.ante.translatedPage.label
                    label: mw.message(`deputy.ante.${type}.label`, notice.name).text()
                });
            }
            if (options.length === 0) {
                options.push({
                    data: null,
                    label: mw.msg('deputy.ante.merge.from.empty'),
                    disabled: true
                });
                mergeTargetButton.setDisabled(true);
                mergeAllButton.setDisabled(true);
            }
            else {
                mergeTargetButton.setDisabled(false);
                mergeAllButton.setDisabled(false);
            }
            mergeTarget.setOptions(options);
        };
        mergePanel.on('toggle', recalculateOptions);
        mergePanel.addItems([mergeFieldLayout, mergeAllButton]);
        return unwrapWidget(mergePanel);
    }
    /**
     * Renders the preview "panel". Not an actual panel, but rather a <div> that
     * shows a preview of the template to be saved. Automatically updates on
     * template changes.
     *
     * @param template The notice to generate previews for and listen events on.
     * @return A preview panel that automatically updates based on the provided notice.
     */
    function renderPreviewPanel(template) {
        const previewPanel = h_1("div", { class: "cte-preview" });
        // TODO: types-mediawiki limitation
        const updatePreview = mw.util.throttle(() => __awaiter(this, void 0, void 0, function* () {
            if (!previewPanel) {
                // Skip if still unavailable.
                return;
            }
            yield template.generatePreview().then((data) => {
                previewPanel.innerHTML = data;
                // Make all anchor links open in a new tab (prevents exit navigation)
                previewPanel.querySelectorAll('a')
                    .forEach((el) => {
                    el.setAttribute('target', '_blank');
                    el.setAttribute('rel', 'noopener');
                });
                // Infuse collapsibles
                $(previewPanel).find('.mw-collapsible')
                    .makeCollapsible();
                $(previewPanel).find('.collapsible')
                    .each((i, e) => {
                    $(e).makeCollapsible({
                        collapsed: e.classList.contains('collapsed')
                    });
                });
            });
        }), 1000);
        // Listen for changes
        template.addEventListener('save', () => {
            updatePreview();
        });
        updatePreview();
        return previewPanel;
    }

    let InternalCopiedTemplatePage;
    /**
     * UI representation of a {{copied}} template. This representation is further broken
     * down with `CopiedTemplateRowPage`, which represents each row on the template.
     *
     * Note that "Page" in the class title does not refer to a MediaWiki page, but rather
     * a OOUI PageLayout.
     */
    function initCopiedTemplatePage() {
        InternalCopiedTemplatePage = class CopiedTemplatePage extends OO.ui.PageLayout {
            /**
             * @param config Configuration to be passed to the element.
             */
            constructor(config) {
                const { copiedTemplate, parent } = config;
                if (parent == null) {
                    throw new Error('Parent dialog (CopiedTemplateEditorDialog) is required');
                }
                else if (copiedTemplate == null) {
                    throw new Error('Reference template (CopiedTemplate) is required');
                }
                const label = mw.message('deputy.ante.copied.label', config.copiedTemplate.name).text();
                const finalConfig = {
                    label: label,
                    classes: ['cte-page-template']
                };
                super(copiedTemplate.id, finalConfig);
                /**
                 * All child pages of this CopiedTemplatePage. Garbage collected when rechecked.
                 */
                this.childPages = new Map();
                this.document = config.copiedTemplate.parsoid;
                this.copiedTemplate = config.copiedTemplate;
                this.parent = config.parent;
                this.label = label;
                copiedTemplate.addEventListener('rowAdd', () => {
                    parent.rebuildPages();
                });
                copiedTemplate.addEventListener('rowDelete', () => {
                    parent.rebuildPages();
                });
                copiedTemplate.addEventListener('destroy', () => {
                    parent.rebuildPages();
                });
                this.$element.append(this.renderButtons(), this.renderHeader(), renderMergePanel('copied', this.copiedTemplate, this.mergeButton), renderPreviewPanel(this.copiedTemplate), this.renderTemplateOptions());
            }
            /**
             * @inheritDoc
             */
            getChildren() {
                const rows = this.copiedTemplate.rows;
                const rowPages = [];
                for (const row of rows) {
                    if (!this.childPages.has(row)) {
                        this.childPages.set(row, row.generatePage(this.parent));
                    }
                    rowPages.push(this.childPages.get(row));
                }
                // Delete deleted rows from cache.
                this.childPages.forEach((page, row) => {
                    if (rowPages.indexOf(page) === -1) {
                        this.childPages.delete(row);
                    }
                });
                return rowPages;
            }
            /**
             * @return The rendered header of this PageLayout.
             */
            renderHeader() {
                return h_1("h3", null, this.label);
            }
            /**
             * Renders the set of buttons that appear at the top of the page.
             *
             * @return A <div> element.
             */
            renderButtons() {
                const buttonSet = h_1("div", { style: { float: 'right' } });
                this.mergeButton = new OO.ui.ButtonWidget({
                    icon: 'tableMergeCells',
                    title: mw.msg('deputy.ante.merge'),
                    framed: false
                });
                const deleteButton = new OO.ui.ButtonWidget({
                    icon: 'trash',
                    title: mw.msg('deputy.ante.copied.remove'),
                    framed: false,
                    flags: ['destructive']
                });
                deleteButton.on('click', () => {
                    if (this.copiedTemplate.rows.length > 0) {
                        dangerModeConfirm(window.CopiedTemplateEditor.config, mw.message('deputy.ante.copied.remove.confirm', `${this.copiedTemplate.rows.length}`).text()).done((confirmed) => {
                            if (confirmed) {
                                this.copiedTemplate.destroy();
                            }
                        });
                    }
                    else {
                        this.copiedTemplate.destroy();
                    }
                });
                const addButton = new OO.ui.ButtonWidget({
                    flags: ['progressive'],
                    icon: 'add',
                    label: mw.msg('deputy.ante.copied.add')
                });
                addButton.on('click', () => {
                    this.copiedTemplate.addRow(new CopiedTemplateRow({
                        to: new mw.Title(this.document.getPage()).getSubjectPage().getPrefixedText()
                    }, this.copiedTemplate));
                });
                buttonSet.appendChild(unwrapWidget(this.mergeButton));
                buttonSet.appendChild(unwrapWidget(deleteButton));
                buttonSet.appendChild(unwrapWidget(addButton));
                return buttonSet;
            }
            /**
             * Renders the panel used to merge multiple {{copied}} templates.
             *
             * @return A <div> element
             */
            renderMergePanel() {
                return renderMergePanel('copied', this.copiedTemplate, this.mergeButton);
            }
            /**
             * Renders the global options of this template. This includes parameters that are not
             * counted towards an entry and affect the template as a whole.
             *
             * @return A <div> element.
             */
            renderTemplateOptions() {
                var _a, _b;
                this.inputSet = {
                    collapse: new OO.ui.CheckboxInputWidget({
                        selected: yesNo((_a = this.copiedTemplate.collapsed) === null || _a === void 0 ? void 0 : _a.trim(), false)
                    }),
                    small: new OO.ui.CheckboxInputWidget({
                        selected: yesNo((_b = this.copiedTemplate.small) === null || _b === void 0 ? void 0 : _b.trim(), false)
                    })
                };
                this.fields = {
                    collapse: new OO.ui.FieldLayout(this.inputSet.collapse, {
                        label: mw.msg('deputy.ante.copied.collapse'),
                        align: 'inline'
                    }),
                    small: new OO.ui.FieldLayout(this.inputSet.small, {
                        label: mw.msg('deputy.ante.copied.small'),
                        align: 'inline'
                    })
                };
                this.inputSet.collapse.on('change', (value) => {
                    this.copiedTemplate.collapsed = value ? 'yes' : null;
                    this.copiedTemplate.save();
                });
                this.inputSet.small.on('change', (value) => {
                    this.copiedTemplate.small = value ? 'yes' : null;
                    this.copiedTemplate.save();
                });
                return h_1("div", { class: "cte-templateOptions" },
                    h_1("div", null, unwrapWidget(this.fields.collapse)),
                    h_1("div", null, unwrapWidget(this.fields.small)));
            }
            /**
             * Sets up the outline item of this page. Used in the BookletLayout.
             */
            setupOutlineItem() {
                if (this.outlineItem !== undefined) {
                    this.outlineItem
                        .setMovable(true)
                        .setRemovable(true)
                        .setIcon('puzzle')
                        .setLevel(0)
                        .setLabel(this.label);
                }
            }
        };
    }
    /**
     * Creates a new CopiedTemplatePage.
     *
     * @param config Configuration to be passed to the element.
     * @return A CopiedTemplatePage object
     */
    function CopiedTemplatePage (config) {
        if (!InternalCopiedTemplatePage) {
            initCopiedTemplatePage();
        }
        return new InternalCopiedTemplatePage(config);
    }

    /**
     * Renders wikitext as HTML.
     *
     * @param wikitext
     * @param title
     * @param options
     */
    function renderWikitext(wikitext, title, options = {}) {
        return __awaiter(this, void 0, void 0, function* () {
            return MwApi.action.post(Object.assign({
                action: 'parse',
                title: title,
                text: wikitext,
                preview: true,
                disableeditsection: true,
                disablelimitreport: true
            }, options)).then((data) => {
                return Object.assign(data.parse.text, {
                    summary: data.parse.parsedsummary
                });
            });
        });
    }

    /**
     * The AttributionNotice abstract class serves as the blueprint for other
     * subclasses that are instances of AttributionNotices (e.g {@link CopiedTemplate}).
     * It provides the basic functionality for the processing of attribution notices.
     */
    class AttributionNotice extends EventTarget {
        /**
         * @return The ParsoidDocument handling this notice (specifically its node).
         */
        get parsoid() {
            return this.node.parsoidDocument;
        }
        /**
         * @return The HTMLElement of the node
         */
        get element() {
            return this.node.element;
        }
        /**
         * @return This template's `i` variable, used to identify this template in
         * the template's `parts` (`data-mw`).
         */
        get i() {
            return this.node.i;
        }
        /**
         * Super constructor for AttributionNotice subclasses.
         *
         * @param node
         *        The ParsoidTransclusionTemplateNode of this notice.
         */
        constructor(node) {
            super();
            this.node = node;
            this.name = this.element.getAttribute('about')
                .replace(/^#mwt/, '') + '-' + this.i;
            this.id = window.btoa(node.getTarget().wt) + '-' + this.name;
            this.parse();
        }
        /**
         * Provides low-level access to a template's `data-mw` entry. When possible,
         * use functions from `.node` instead, as these are much more stable.
         *
         * @param callback The callback for data-modifying operations.
         */
        accessTemplateData(callback) {
            const jsonData = JSON.parse(this.element.getAttribute('data-mw'));
            let templateData;
            let index;
            jsonData.parts.forEach((v, k) => {
                if (v != null && v.template !== undefined && v.template.i === this.i) {
                    templateData = v;
                    index = k;
                }
            });
            if (templateData === undefined) {
                throw new TypeError('Invalid `i` given to template.');
            }
            templateData = callback(templateData);
            if (templateData === undefined) {
                jsonData.parts.splice(index, 1);
            }
            else {
                jsonData.parts[index] = templateData;
            }
            this.element.setAttribute('data-mw', JSON.stringify(jsonData));
            if (jsonData.parts.length === 0) {
                this.parsoid.getDocument().querySelectorAll(`[about="${this.element.getAttribute('about')}"]`).forEach((e) => {
                    e.parentElement.removeChild(e);
                });
            }
        }
        /**
         * Gets a wikitext string representation of this template. Used for
         * previews.
         *
         * @return wikitext.
         */
        toWikitext() {
            let wikitext = '{{';
            this.accessTemplateData((data) => {
                wikitext += data.template.target.wt;
                for (const key in data.template.params) {
                    if (!Object.hasOwnProperty.call(data.template.params, key)) {
                        continue;
                    }
                    const value = data.template.params[key];
                    wikitext += `| ${key} = ${value.wt}\n`;
                }
                return data;
            });
            return wikitext + '}}';
        }
        /**
         * Converts this notice to parsed HTML.
         *
         * @return {Promise<string>}
         */
        generatePreview() {
            return __awaiter(this, void 0, void 0, function* () {
                return renderWikitext(this.toWikitext(), this.parsoid.getPage());
            });
        }
    }

    /**
     * An event that reflects a change in a given {{copied}} template
     * row.
     */
    class RowChangeEvent extends Event {
        /**
         * Creates a new RowChangeEvent.
         *
         * @param type The event type.
         * @param row The changed row.
         */
        constructor(type, row) {
            super(type);
            this.row = row;
        }
    }

    /**
     * This is a sub-abstract class of {@link AttributionNotice} that represents any
     * attribution notice template that can contain multiple entries (or rows).
     */
    class RowedAttributionNotice extends AttributionNotice {
        /**
         * @return This template's rows.
         */
        get rows() {
            return this._rows;
        }
        /**
         * Checks if this current template has row parameters with a given suffix, or no
         * suffix if not supplied.
         *
         * @param parameters The parameter names to check for
         * @param suffix The suffix of the parameter
         * @return `true` if parameters exist
         * @private
         */
        hasRowParameters(parameters, suffix = '') {
            return Object.keys(this.node.getParameters()).some((v) => parameters.map((v2) => `${v2}${suffix}`)
                .indexOf(v) !== -1);
        }
        /**
         * Extracts parameters from `this.node` and returns a row.
         *
         * @param parameters The parameter names to check for
         * @param suffix The suffix of the parameter
         * @return A row, or null if no parameters were found.
         * @private
         */
        extractRowParameters(parameters, suffix = '') {
            const row = {};
            parameters.forEach((key) => {
                if (this.node.hasParameter(key + suffix) !== undefined) {
                    row[key] = this.node.getParameter(key + suffix);
                }
                else if (suffix === '' && this.node.hasParameter(`${key}1`)) {
                    // Non-numbered parameter not found but a numbered parameter with
                    // an index of 1 was found. Fall back to that value.
                    row[key] = this.node.getParameter(`${key}1`).trim();
                }
                else if (suffix === 1 && this.node.hasParameter(`${key}`)) {
                    // This is i = 1, so fall back to a non-numbered parameter (if exists)
                    const unnumberedParamValue = this.node.getParameter(`${key}`).trim();
                    if (unnumberedParamValue !== undefined) {
                        row[key] = unnumberedParamValue;
                    }
                }
            });
            return row;
        }
        /**
         * Adds a row to this template.
         *
         * @param row The row to add.
         */
        addRow(row) {
            this._rows.push(row);
            this.save();
            this.dispatchEvent(new RowChangeEvent('rowAdd', row));
        }
        /**
         * Deletes a row to this template.
         *
         * @param row The row to delete.
         */
        deleteRow(row) {
            const i = this._rows.findIndex((v) => v === row);
            if (i !== -1) {
                this._rows.splice(i, 1);
                this.save();
                this.dispatchEvent(new RowChangeEvent('rowDelete', row));
            }
            if (this._rows.length === 0) {
                this.destroy();
            }
        }
        /**
         * Copies in the rows of another {@link SplitArticleTemplate}, and
         * optionally deletes that template or clears its contents.
         *
         * @param template The template to copy from.
         * @param options Options for this merge.
         * @param options.delete
         *        Whether the reference template will be deleted after merging.
         * @param options.clear
         *        Whether the reference template's rows will be cleared after merging.
         */
        merge(template, options = {}) {
            if (template.rows === undefined || template === this) {
                // Deleted or self
                return;
            }
            for (const row of template.rows) {
                if (options.clear) {
                    row.parent = this;
                }
                else {
                    this.addRow(row.clone(this));
                }
            }
            if (options.delete) {
                template.destroy();
            }
        }
    }

    /**
     * Represents a single {{copied}} template in the Parsoid document.
     */
    class CopiedTemplate extends RowedAttributionNotice {
        /**
         * @return This template's rows.
         */
        get rows() {
            return this._rows;
        }
        /**
         * @inheritDoc
         */
        parse() {
            if (this.node.getParameter('collapse')) {
                this.collapsed = this.node.getParameter('collapse');
            }
            if (this.node.getParameter('small')) {
                this.small = this.node.getParameter('small');
            }
            // Extract {{copied}} rows.
            const rows = [];
            // Numberless
            if (this.hasRowParameters(copiedTemplateRowParameters)) {
                // If `from`, `to`, ..., or `merge` is found.
                rows.push(new CopiedTemplateRow(this.extractRowParameters(copiedTemplateRowParameters), this));
            }
            // Numbered
            let i = 1, continueExtracting = true;
            do {
                if (this.hasRowParameters(copiedTemplateRowParameters, i)) {
                    rows.push(new CopiedTemplateRow(this.extractRowParameters(copiedTemplateRowParameters, i), this));
                }
                else if (!(i === 1 && rows.length > 0)) {
                    // Row doesn't exist. Stop parsing from here.
                    continueExtracting = false;
                }
                i++;
            } while (continueExtracting);
            /**
             * All the rows of this template.
             *
             * @type {CopiedTemplateRow[]}
             */
            this._rows = rows;
        }
        /**
         * @inheritDoc
         */
        save() {
            if (this.collapsed !== undefined) {
                this.node.setParameter('collapse', yesNo(this.collapsed) ? 'yes' : null);
            }
            if (this.small !== undefined) {
                this.node.setParameter('small', yesNo(this.small) ? 'yes' : null);
            }
            const existingParameters = this.node.getParameters();
            for (const param in existingParameters) {
                if (copiedTemplateRowParameters.some((v) => param.startsWith(v))) {
                    // This is a row parameter. Remove it in preparation for rebuild (further below).
                    this.node.removeParameter(param);
                }
            }
            if (this._rows.length === 1) {
                // If there is only one row, don't bother with numbered rows.
                for (const param of copiedTemplateRowParameters) {
                    if (this._rows[0][param] !== undefined) {
                        this.node.setParameter(param, this._rows[0][param]);
                    }
                }
            }
            else {
                // If there are multiple rows, add number suffixes (except for i = 0).
                for (let i = 0; i < this._rows.length; i++) {
                    for (const param of copiedTemplateRowParameters) {
                        if (this._rows[i][param] !== undefined) {
                            this.node.setParameter(param + (i === 0 ? '' : i + 1), this._rows[i][param]);
                        }
                    }
                }
            }
            this.dispatchEvent(new Event('save'));
        }
        /**
         * @inheritDoc
         */
        destroy() {
            this.node.destroy();
            // Self-destruct
            Object.keys(this).forEach((k) => delete this[k]);
            this.dispatchEvent(new Event('destroy'));
        }
        /**
         * @inheritDoc
         */
        generatePage(dialog) {
            return CopiedTemplatePage({
                copiedTemplate: this,
                parent: dialog
            });
        }
        /**
         * Copies in the rows of another {@link CopiedTemplate}, and
         * optionally deletes that template or clears its contents.
         *
         * @param template The template to copy from.
         * @param options Options for this merge.
         * @param options.delete
         *        Whether the reference template will be deleted after merging.
         * @param options.clear
         *        Whether the reference template's rows will be cleared after merging.
         */
        merge(template, options = {}) {
            if (template.rows === undefined || template === this) {
                // Deleted or self
                return;
            }
            for (const row of template.rows) {
                if (options.clear) {
                    row.parent = this;
                }
                else {
                    this.addRow(row.clone(this));
                }
            }
            if (options.delete) {
                template.destroy();
            }
        }
    }

    let InternalSplitArticleTemplateRowPage;
    /**
     * Initializes the process element.
     */
    function initSplitArticleTemplateRowPage() {
        InternalSplitArticleTemplateRowPage = class SplitArticleTemplateRowPage extends OO.ui.PageLayout {
            /**
             * @param config Configuration to be passed to the element.
             */
            constructor(config) {
                const { splitArticleTemplateRow, parent } = config;
                if (parent == null) {
                    throw new Error('Parent dialog (CopiedTemplateEditorDialog) is required');
                }
                else if (splitArticleTemplateRow == null) {
                    throw new Error('Reference row (SplitArticleTemplateRow) is required');
                }
                const finalConfig = {
                    classes: ['cte-page-row']
                };
                super(splitArticleTemplateRow.id, finalConfig);
                this.parent = parent;
                this.splitArticleTemplateRow = splitArticleTemplateRow;
                this.refreshLabel();
                this.splitArticleTemplateRow.parent.addEventListener('destroy', () => {
                    parent.rebuildPages();
                });
                this.splitArticleTemplateRow.parent.addEventListener('rowDelete', () => {
                    parent.rebuildPages();
                });
                this.$element.append(this.render().$element);
            }
            /**
             * Refreshes the page's label
             */
            refreshLabel() {
                this.label = mw.message('deputy.ante.splitArticle.entry.short', this.splitArticleTemplateRow.to || '???', this.splitArticleTemplateRow.date || '???').text();
                if (this.outlineItem) {
                    this.outlineItem.setLabel(this.label);
                }
            }
            /**
             * Renders this page. Returns a FieldsetLayout OOUI widget.
             *
             * @return An OOUI FieldsetLayout
             */
            render() {
                this.layout = new OO.ui.FieldsetLayout({
                    icon: 'parameter',
                    label: mw.msg('deputy.ante.splitArticle.entry.label'),
                    classes: ['cte-fieldset']
                });
                this.layout.$element.append(this.renderButtons());
                this.layout.addItems(this.renderFields());
                return this.layout;
            }
            /**
             * Renders a set of buttons used to modify a specific {{copied}} template row.
             *
             * @return An array of OOUI FieldLayouts
             */
            renderButtons() {
                const deleteButton = new OO.ui.ButtonWidget({
                    icon: 'trash',
                    title: mw.msg('deputy.ante.splitArticle.entry.remove'),
                    framed: false,
                    flags: ['destructive']
                });
                deleteButton.on('click', () => {
                    this.splitArticleTemplateRow.parent.deleteRow(this.splitArticleTemplateRow);
                });
                return h_1("div", { style: {
                        float: 'right',
                        position: 'absolute',
                        top: '0.5em',
                        right: '0.5em'
                    } }, unwrapWidget(deleteButton));
            }
            /**
             * Renders a set of OOUI InputWidgets and FieldLayouts, eventually returning an
             * array of each FieldLayout to append to the FieldsetLayout.
             *
             * @return An array of OOUI FieldLayouts
             */
            renderFields() {
                const rowDate = this.splitArticleTemplateRow.date;
                const parsedDate = (rowDate == null || rowDate.trim().length === 0) ?
                    undefined : (!isNaN(new Date(rowDate.trim() + ' UTC').getTime()) ?
                    (new Date(rowDate.trim() + ' UTC')) : (!isNaN(new Date(rowDate.trim()).getTime()) ?
                    new Date(rowDate.trim()) : null));
                const inputs = {
                    to: SmartTitleInputWidget({
                        $overlay: this.parent.$overlay,
                        required: true,
                        value: this.splitArticleTemplateRow.to || '',
                        placeholder: mw.msg('deputy.ante.splitArticle.to.placeholder')
                    }),
                    // eslint-disable-next-line camelcase
                    from_oldid: new OO.ui.TextInputWidget({
                        value: this.splitArticleTemplateRow.from_oldid || '',
                        placeholder: mw.msg('deputy.ante.splitArticle.from_oldid.placeholder')
                    }),
                    diff: new OO.ui.TextInputWidget({
                        value: this.splitArticleTemplateRow.from_oldid || '',
                        placeholder: mw.msg('deputy.ante.splitArticle.diff.placeholder'),
                        validate: (value) => {
                            if (
                            // Blank
                            value.trim().length === 0 ||
                                // Diff number
                                !isNaN(+value)) {
                                return true;
                            }
                            try {
                                return typeof new URL(value).href === 'string';
                            }
                            catch (e) {
                                return false;
                            }
                        }
                    }),
                    date: new mw.widgets.DateInputWidget({
                        $overlay: this.parent.$overlay,
                        required: true,
                        icon: 'calendar',
                        value: parsedDate ? `${parsedDate.getUTCFullYear()}-${parsedDate.getUTCMonth() + 1}-${parsedDate.getUTCDate()}` : undefined,
                        placeholder: mw.msg('deputy.ante.copied.date.placeholder'),
                        calendar: {
                            verticalPosition: 'above'
                        }
                    })
                };
                const dateAuto = RevisionDateGetButton({
                    label: mw.msg('deputy.ante.dateAuto', 'diff'),
                    revisionInputWidget: inputs.diff,
                    dateInputWidget: inputs.date
                });
                const fieldLayouts = {
                    to: new OO.ui.FieldLayout(inputs.to, {
                        $overlay: this.parent.$overlay,
                        align: 'top',
                        label: mw.msg('deputy.ante.splitArticle.to.label'),
                        help: mw.msg('deputy.ante.splitArticle.to.help')
                    }),
                    // eslint-disable-next-line camelcase
                    from_oldid: new OO.ui.FieldLayout(inputs.from_oldid, {
                        $overlay: this.parent.$overlay,
                        align: 'left',
                        label: mw.msg('deputy.ante.splitArticle.from_oldid.label'),
                        help: mw.msg('deputy.ante.splitArticle.from_oldid.help')
                    }),
                    diff: new OO.ui.FieldLayout(inputs.diff, {
                        $overlay: this.parent.$overlay,
                        align: 'left',
                        label: mw.msg('deputy.ante.splitArticle.diff.label'),
                        help: mw.msg('deputy.ante.splitArticle.diff.help')
                    }),
                    date: new OO.ui.ActionFieldLayout(inputs.date, dateAuto, {
                        $overlay: this.parent.$overlay,
                        align: 'left',
                        label: mw.msg('deputy.ante.splitArticle.date.label'),
                        help: mw.msg('deputy.ante.splitArticle.date.help')
                    })
                };
                for (const _field in inputs) {
                    const field = _field;
                    const input = inputs[field];
                    // Attach the change listener
                    input.on('change', (value) => {
                        if (input instanceof mw.widgets.DateInputWidget) {
                            this.splitArticleTemplateRow[field] = value ?
                                window.moment(value, 'YYYY-MM-DD')
                                    .locale(mw.config.get('wgContentLanguage'))
                                    .format('D MMMM Y') : undefined;
                            if (value.length > 0) {
                                fieldLayouts[field].setWarnings([]);
                            }
                        }
                        else {
                            this.splitArticleTemplateRow[field] = value;
                        }
                        this.splitArticleTemplateRow.parent.save();
                    });
                    if (input instanceof OO.ui.TextInputWidget) {
                        // Rechecks the validity of the field.
                        input.setValidityFlag();
                    }
                }
                inputs.to.on('change', () => {
                    this.refreshLabel();
                });
                inputs.date.on('change', () => {
                    this.refreshLabel();
                });
                return getObjectValues(fieldLayouts);
            }
            /**
             * Sets up the outline item of this page. Used in the BookletLayout.
             */
            setupOutlineItem() {
                if (this.outlineItem !== undefined) {
                    this.outlineItem
                        .setMovable(true)
                        .setRemovable(true)
                        .setIcon('parameter')
                        .setLevel(1)
                        .setLabel(this.label);
                }
            }
        };
    }
    /**
     * Creates a new SplitArticleTemplateRowPage.
     *
     * @param config Configuration to be passed to the element.
     * @return A SplitArticleTemplateRowPage object
     */
    function SplitArticleTemplateRowPage (config) {
        if (!InternalSplitArticleTemplateRowPage) {
            initSplitArticleTemplateRowPage();
        }
        return new InternalSplitArticleTemplateRowPage(config);
    }

    // noinspection JSDeprecatedSymbols
    const splitArticleTemplateRowParameters = [
        'to', 'from_oldid', 'date', 'diff'
    ];
    /**
     * Represents a row/entry in a {{split article}} template.
     */
    class SplitArticleTemplateRow extends AttributionNoticeRow {
        /**
         * Creates a new RawCopiedTemplateRow
         *
         * @param rowObjects
         * @param parent
         */
        constructor(rowObjects, parent) {
            super(parent);
            this.to = rowObjects.to;
            // eslint-disable-next-line camelcase
            this.from_oldid = rowObjects.from_oldid;
            this.date = rowObjects.date;
            this.diff = rowObjects.diff;
            this._parent = parent;
        }
        /**
         * @inheritDoc
         */
        clone(parent) {
            return super.clone(parent);
        }
        /**
         * @inheritDoc
         */
        generatePage(dialog) {
            return SplitArticleTemplateRowPage({
                splitArticleTemplateRow: this,
                parent: dialog
            });
        }
    }

    let InternalSplitArticleTemplatePage;
    /**
     * Initializes the process element.
     */
    function initSplitArticleTemplatePage() {
        InternalSplitArticleTemplatePage = class SplitArticleTemplatePage extends OO.ui.PageLayout {
            /**
             * @param config Configuration to be passed to the element.
             */
            constructor(config) {
                const { splitArticleTemplate, parent } = config;
                if (parent == null) {
                    throw new Error('Parent dialog (CopiedTemplateEditorDialog) is required');
                }
                else if (splitArticleTemplate == null) {
                    throw new Error('Reference template (SplitArticleTemplate) is required');
                }
                const label = mw.message('deputy.ante.splitArticle.label', config.splitArticleTemplate.name).text();
                const finalConfig = {
                    label: label,
                    classes: ['cte-page-template']
                };
                super(splitArticleTemplate.id, finalConfig);
                /**
                 * All child pages of this splitArticleTemplatePage. Garbage collected when rechecked.
                 */
                this.childPages = new Map();
                this.document = config.splitArticleTemplate.parsoid;
                this.splitArticleTemplate = config.splitArticleTemplate;
                this.parent = config.parent;
                this.label = label;
                splitArticleTemplate.addEventListener('rowAdd', () => {
                    parent.rebuildPages();
                });
                splitArticleTemplate.addEventListener('rowDelete', () => {
                    parent.rebuildPages();
                });
                splitArticleTemplate.addEventListener('destroy', () => {
                    parent.rebuildPages();
                });
                this.$element.append(this.renderButtons(), this.renderHeader(), renderMergePanel('splitArticle', this.splitArticleTemplate, this.mergeButton), renderPreviewPanel(this.splitArticleTemplate), this.renderTemplateOptions());
            }
            /**
             * @inheritDoc
             */
            getChildren() {
                const rows = this.splitArticleTemplate.rows;
                const rowPages = [];
                for (const row of rows) {
                    if (!this.childPages.has(row)) {
                        this.childPages.set(row, row.generatePage(this.parent));
                    }
                    rowPages.push(this.childPages.get(row));
                }
                // Delete deleted rows from cache.
                this.childPages.forEach((page, row) => {
                    if (rowPages.indexOf(page) === -1) {
                        this.childPages.delete(row);
                    }
                });
                return rowPages;
            }
            /**
             * Renders the set of buttons that appear at the top of the page.
             *
             * @return A <div> element.
             */
            renderButtons() {
                const buttonSet = h_1("div", { style: { float: 'right' } });
                this.mergeButton = new OO.ui.ButtonWidget({
                    icon: 'tableMergeCells',
                    title: mw.msg('deputy.ante.merge'),
                    framed: false
                });
                const deleteButton = new OO.ui.ButtonWidget({
                    icon: 'trash',
                    title: mw.msg('deputy.ante.splitArticle.remove'),
                    framed: false,
                    flags: ['destructive']
                });
                deleteButton.on('click', () => {
                    if (this.splitArticleTemplate.rows.length > 0) {
                        dangerModeConfirm(window.CopiedTemplateEditor.config, mw.message('deputy.ante.splitArticle.remove.confirm', `${this.splitArticleTemplate.rows.length}`).text()).done((confirmed) => {
                            if (confirmed) {
                                this.splitArticleTemplate.destroy();
                            }
                        });
                    }
                    else {
                        this.splitArticleTemplate.destroy();
                    }
                });
                const addButton = new OO.ui.ButtonWidget({
                    flags: ['progressive'],
                    icon: 'add',
                    label: mw.msg('deputy.ante.splitArticle.add')
                });
                addButton.on('click', () => {
                    this.splitArticleTemplate.addRow(new SplitArticleTemplateRow({}, this.splitArticleTemplate));
                });
                this.splitArticleTemplate.addEventListener('rowAdd', () => {
                    // TODO: Remove after template improvements.
                    addButton.setDisabled(this.splitArticleTemplate.rows.length >= 10);
                });
                buttonSet.appendChild(unwrapWidget(this.mergeButton));
                buttonSet.appendChild(unwrapWidget(deleteButton));
                buttonSet.appendChild(unwrapWidget(addButton));
                return buttonSet;
            }
            /**
             * @return The rendered header of this PageLayout.
             */
            renderHeader() {
                return h_1("h3", null, this.label);
            }
            /**
             * Renders the global options of this template. This includes parameters that are not
             * counted towards an entry and affect the template as a whole.
             *
             * @return A <div> element.
             */
            renderTemplateOptions() {
                const page = new mw.Title(this.splitArticleTemplate.parsoid.getPage()).getSubjectPage().getPrefixedText();
                const collapse = new OO.ui.CheckboxInputWidget({
                    selected: this.splitArticleTemplate.collapse ?
                        yesNo(this.splitArticleTemplate.collapse) : false
                });
                const from = SmartTitleInputWidget({
                    $overlay: this.parent.$overlay,
                    value: this.splitArticleTemplate.from || '',
                    placeholder: page
                });
                collapse.on('change', (value) => {
                    this.splitArticleTemplate.collapse = value ? 'yes' : null;
                    this.splitArticleTemplate.save();
                });
                from.on('change', (value) => {
                    this.splitArticleTemplate.from = value.length > 0 ? value : page;
                    this.splitArticleTemplate.save();
                });
                return h_1("div", { class: "cte-templateOptions" },
                    h_1("div", null, unwrapWidget(new OO.ui.FieldLayout(from, {
                        $overlay: this.parent.$overlay,
                        align: 'top',
                        label: mw.msg('deputy.ante.splitArticle.from'),
                        help: mw.msg('deputy.ante.splitArticle.from.help')
                    }))),
                    h_1("div", { style: {
                            flex: '0',
                            alignSelf: 'center',
                            marginLeft: '8px'
                        } }, unwrapWidget(new OO.ui.FieldLayout(collapse, {
                        $overlay: this.parent.$overlay,
                        align: 'top',
                        label: mw.msg('deputy.ante.splitArticle.collapse')
                    }))));
            }
            /**
             * Sets up the outline item of this page. Used in the BookletLayout.
             */
            setupOutlineItem() {
                if (this.outlineItem !== undefined) {
                    this.outlineItem
                        .setMovable(true)
                        .setRemovable(true)
                        .setIcon('puzzle')
                        .setLevel(0)
                        .setLabel(this.label);
                }
            }
        };
    }
    /**
     * Creates a new SplitArticleTemplatePage.
     *
     * @param config Configuration to be passed to the element.
     * @return A SplitArticleTemplatePage object
     */
    function SplitArticleTemplatePage (config) {
        if (!InternalSplitArticleTemplatePage) {
            initSplitArticleTemplatePage();
        }
        return new InternalSplitArticleTemplatePage(config);
    }

    /**
     * Represents a single {{split article}} template.
     */
    class SplitArticleTemplate extends RowedAttributionNotice {
        /**
         * @inheritDoc
         */
        parse() {
            if (this.node.hasParameter('from')) {
                this.from = this.node.getParameter('from');
            }
            if (this.node.hasParameter('collapse')) {
                this.collapse = this.node.getParameter('collapse');
            }
            // Extract {{copied}} rows.
            const rows = [];
            // Numberless
            if (this.hasRowParameters(splitArticleTemplateRowParameters)) {
                // If `from`, `to`, ..., or `merge` is found.
                rows.push(new SplitArticleTemplateRow(this.extractRowParameters(splitArticleTemplateRowParameters), this));
            }
            // Numbered
            let i = 1, continueExtracting = true;
            do {
                if (this.hasRowParameters(splitArticleTemplateRowParameters, i)) {
                    rows.push(new SplitArticleTemplateRow(this.extractRowParameters(splitArticleTemplateRowParameters, i), this));
                }
                else if (!(i === 1 && rows.length > 0)) {
                    // Row doesn't exist. Stop parsing from here.
                    continueExtracting = false;
                }
                i++;
                // Hard limit to `i` added due to the template's construction.
                // TODO: Modify template to allow more than 10.
            } while (continueExtracting && i <= 10);
            this._rows = rows;
        }
        /**
         * @inheritDoc
         */
        save() {
            if (this.collapse !== undefined) {
                this.node.setParameter('collapse', yesNo(this.collapse) ? 'yes' : null);
            }
            this.node.setParameter('from', this.from);
            const existingParameters = this.node.getParameters();
            for (const param in existingParameters) {
                if (splitArticleTemplateRowParameters.some((v) => param.startsWith(v))) {
                    // This is a row parameter. Remove it in preparation for rebuild (further below).
                    this.node.removeParameter(param);
                }
            }
            this._rows.forEach((row, i) => {
                this.node.setParameter(`to${i > 0 ? i + 1 : ''}`, row.to);
                this.node.setParameter(`from_oldid${i > 0 ? i + 1 : ''}`, row.from_oldid);
                this.node.setParameter(`date${i > 0 ? i + 1 : ''}`, row.date);
                this.node.setParameter(`diff${i > 0 ? i + 1 : ''}`, row.diff);
            });
            this.dispatchEvent(new Event('save'));
        }
        /**
         *
         * @inheritDoc
         */
        destroy() {
            this.node.destroy();
            // Self-destruct
            Object.keys(this).forEach((k) => delete this[k]);
            this.dispatchEvent(new Event('destroy'));
        }
        /**
         * @inheritDoc
         */
        generatePage(dialog) {
            return SplitArticleTemplatePage({
                splitArticleTemplate: this,
                parent: dialog
            });
        }
    }

    let InternalMergedFromTemplatePage;
    /**
     * Initializes the process element.
     */
    function initMergedFromTemplatePage() {
        InternalMergedFromTemplatePage = class MergedFromTemplatePage extends OO.ui.PageLayout {
            /**
             * @param config Configuration to be passed to the element.
             */
            constructor(config) {
                const { mergedFromTemplate, parent } = config;
                if (parent == null) {
                    throw new Error('Parent dialog (CopiedTemplateEditorDialog) is required');
                }
                else if (mergedFromTemplate == null) {
                    throw new Error('Reference template (MergedFromTemplate) is required');
                }
                const finalConfig = {
                    classes: ['cte-page-template']
                };
                super(mergedFromTemplate.id, finalConfig);
                this.document = mergedFromTemplate.parsoid;
                this.mergedFromTemplate = mergedFromTemplate;
                this.parent = config.parent;
                this.refreshLabel();
                mergedFromTemplate.addEventListener('destroy', () => {
                    parent.rebuildPages();
                });
                this.$element.append(this.renderButtons(), this.renderHeader(), renderPreviewPanel(this.mergedFromTemplate), this.renderTemplateOptions());
            }
            /**
             * Refreshes the page's label
             */
            refreshLabel() {
                this.label = mw.message('deputy.ante.mergedFrom.label', this.mergedFromTemplate.article || '???').text();
                if (this.outlineItem) {
                    this.outlineItem.setLabel(this.label);
                }
            }
            /**
             * Renders the set of buttons that appear at the top of the page.
             *
             * @return A <div> element.
             */
            renderButtons() {
                const buttonSet = h_1("div", { style: { float: 'right' } });
                const deleteButton = new OO.ui.ButtonWidget({
                    icon: 'trash',
                    title: mw.msg('deputy.ante.mergedFrom.remove'),
                    framed: false,
                    flags: ['destructive']
                });
                deleteButton.on('click', () => {
                    this.mergedFromTemplate.destroy();
                });
                buttonSet.appendChild(unwrapWidget(deleteButton));
                return buttonSet;
            }
            /**
             * @return The rendered header of this PageLayout.
             */
            renderHeader() {
                return h_1("h3", null, this.label);
            }
            /**
             * @return The options for this template
             */
            renderTemplateOptions() {
                const layout = new OO.ui.FieldsetLayout({
                    icon: 'parameter',
                    label: mw.msg('deputy.ante.templateOptions'),
                    classes: ['cte-fieldset']
                });
                const rowDate = this.mergedFromTemplate.date;
                const parsedDate = (rowDate == null || rowDate.trim().length === 0) ?
                    undefined : (!isNaN(new Date(rowDate.trim() + ' UTC').getTime()) ?
                    (new Date(rowDate.trim() + ' UTC')) : (!isNaN(new Date(rowDate.trim()).getTime()) ?
                    new Date(rowDate.trim()) : null));
                const inputs = {
                    article: SmartTitleInputWidget({
                        $overlay: this.parent.$overlay,
                        required: true,
                        value: this.mergedFromTemplate.article || '',
                        placeholder: mw.msg('deputy.ante.mergedFrom.article.placeholder')
                    }),
                    date: new mw.widgets.DateInputWidget({
                        $overlay: this.parent.$overlay,
                        required: true,
                        icon: 'calendar',
                        value: parsedDate ? `${parsedDate.getUTCFullYear()}-${parsedDate.getUTCMonth() + 1}-${parsedDate.getUTCDate()}` : undefined,
                        placeholder: mw.msg('deputy.ante.copied.date.placeholder')
                    }),
                    target: SmartTitleInputWidget({
                        $overlay: this.parent.$overlay,
                        value: this.mergedFromTemplate.target || '',
                        placeholder: mw.msg('deputy.ante.mergedFrom.target.placeholder')
                    }),
                    afd: new mw.widgets.TitleInputWidget({
                        $overlay: this.parent.$overlay,
                        value: this.mergedFromTemplate.afd || '',
                        placeholder: mw.msg('deputy.ante.mergedFrom.afd.placeholder'),
                        validate: (title) => {
                            // TODO: ANTE l10n
                            return title.trim().length === 0 || title.startsWith(new mw.Title('Articles for deletion/', nsId('wikipedia'))
                                .toText());
                        }
                    }),
                    talk: new OO.ui.CheckboxInputWidget({
                        selected: yesNo(this.mergedFromTemplate.target)
                    })
                };
                const fieldLayouts = {
                    article: new OO.ui.FieldLayout(inputs.article, {
                        $overlay: this.parent.$overlay,
                        align: 'top',
                        label: mw.msg('deputy.ante.mergedFrom.article.label'),
                        help: mw.msg('deputy.ante.mergedFrom.article.help')
                    }),
                    date: new OO.ui.FieldLayout(inputs.date, {
                        $overlay: this.parent.$overlay,
                        align: 'left',
                        label: mw.msg('deputy.ante.mergedFrom.date.label'),
                        help: mw.msg('deputy.ante.mergedFrom.date.help')
                    }),
                    target: new OO.ui.FieldLayout(inputs.target, {
                        $overlay: this.parent.$overlay,
                        align: 'left',
                        label: mw.msg('deputy.ante.mergedFrom.target.label'),
                        help: mw.msg('deputy.ante.mergedFrom.target.help')
                    }),
                    afd: new OO.ui.FieldLayout(inputs.afd, {
                        $overlay: this.parent.$overlay,
                        align: 'left',
                        label: mw.msg('deputy.ante.mergedFrom.afd.label'),
                        help: mw.msg('deputy.ante.mergedFrom.afd.help')
                    }),
                    talk: new OO.ui.FieldLayout(inputs.talk, {
                        $overlay: this.parent.$overlay,
                        align: 'inline',
                        label: mw.msg('deputy.ante.mergedFrom.talk.label'),
                        help: mw.msg('deputy.ante.mergedFrom.talk.help')
                    })
                };
                for (const _field in inputs) {
                    const field = _field;
                    const input = inputs[field];
                    // Attach the change listener
                    input.on('change', (value) => {
                        if (input instanceof OO.ui.CheckboxInputWidget) {
                            this.mergedFromTemplate[field] = value ? 'yes' : 'no';
                        }
                        else if (input instanceof mw.widgets.DateInputWidget) {
                            this.mergedFromTemplate[field] = value ?
                                window.moment(value, 'YYYY-MM-DD')
                                    .locale(mw.config.get('wgContentLanguage'))
                                    .format('D MMMM Y') : undefined;
                            if (value.length > 0) {
                                fieldLayouts[field].setWarnings([]);
                            }
                        }
                        else {
                            this.mergedFromTemplate[field] = value;
                        }
                        this.mergedFromTemplate.save();
                    });
                    if (input instanceof OO.ui.TextInputWidget) {
                        // Rechecks the validity of the field.
                        input.setValidityFlag();
                    }
                }
                inputs.article.on('change', () => {
                    this.refreshLabel();
                });
                layout.addItems(getObjectValues(fieldLayouts));
                return unwrapWidget(layout);
            }
            /**
             * Sets up the outline item of this page. Used in the BookletLayout.
             */
            setupOutlineItem() {
                if (this.outlineItem !== undefined) {
                    this.outlineItem
                        .setMovable(true)
                        .setRemovable(true)
                        .setIcon('puzzle')
                        .setLevel(0)
                        .setLabel(this.label);
                }
            }
        };
    }
    /**
     * Creates a new MergedFromTemplatePage.
     *
     * @param config Configuration to be passed to the element.
     * @return A MergedFromTemplatePage object
     */
    function MergedFromTemplatePage (config) {
        if (!InternalMergedFromTemplatePage) {
            initMergedFromTemplatePage();
        }
        return new InternalMergedFromTemplatePage(config);
    }

    /**
     * Represents a single {{merged-from}} template in the Parsoid document.
     */
    class MergedFromTemplate extends AttributionNotice {
        /**
         * @inheritDoc
         */
        parse() {
            if (this.node.hasParameter('1')) {
                this.article = this.node.getParameter('1');
            }
            if (this.node.hasParameter('2')) {
                this.date = this.node.getParameter('2');
            }
            if (this.node.hasParameter('talk')) {
                this.talk = this.node.getParameter('talk');
            }
            if (this.node.hasParameter('target')) {
                this.target = this.node.getParameter('target');
            }
            if (this.node.hasParameter('afd')) {
                this.afd = this.node.getParameter('afd');
            }
        }
        /**
         * @inheritDoc
         */
        save() {
            var _a, _b;
            this.node.setParameter('1', this.article);
            this.node.setParameter('2', this.date);
            if (this.talk !== undefined) {
                this.node.setParameter('talk', yesNo(this.talk) ? null : 'no');
            }
            this.node.setParameter('target', ((_a = this.target) !== null && _a !== void 0 ? _a : '').length > 0 ? this.target : null);
            this.node.setParameter('afd', ((_b = this.afd) !== null && _b !== void 0 ? _b : '').length > 0 ? this.afd : null);
            this.dispatchEvent(new Event('save'));
        }
        /**
         * @inheritDoc
         */
        destroy() {
            this.node.destroy();
            // Self-destruct
            Object.keys(this).forEach((k) => delete this[k]);
            this.dispatchEvent(new Event('destroy'));
        }
        /**
         * @inheritDoc
         */
        generatePage(dialog) {
            return MergedFromTemplatePage({
                mergedFromTemplate: this,
                parent: dialog
            });
        }
    }

    let InternalMergedToTemplatePage;
    /**
     * Initializes the process element.
     */
    function initMergedToTemplatePage() {
        InternalMergedToTemplatePage = class MergedToTemplatePage extends OO.ui.PageLayout {
            /**
             * @param config Configuration to be passed to the element.
             */
            constructor(config) {
                const { mergedToTemplate, parent } = config;
                if (parent == null) {
                    throw new Error('Parent dialog (CopiedTemplateEditorDialog) is required');
                }
                else if (mergedToTemplate == null) {
                    throw new Error('Reference template (MergedToTemplate) is required');
                }
                const finalConfig = {
                    classes: ['cte-page-template']
                };
                super(mergedToTemplate.id, finalConfig);
                this.document = mergedToTemplate.parsoid;
                this.mergedToTemplate = mergedToTemplate;
                this.parent = config.parent;
                this.refreshLabel();
                mergedToTemplate.addEventListener('destroy', () => {
                    parent.rebuildPages();
                });
                this.$element.append(this.renderButtons(), this.renderHeader(), renderPreviewPanel(this.mergedToTemplate), this.renderTemplateOptions());
            }
            /**
             * Refreshes the page's label
             */
            refreshLabel() {
                this.label = mw.message('deputy.ante.mergedTo.label', this.mergedToTemplate.to || '???').text();
                if (this.outlineItem) {
                    this.outlineItem.setLabel(this.label);
                }
            }
            /**
             * Renders the set of buttons that appear at the top of the page.
             *
             * @return A <div> element.
             */
            renderButtons() {
                const buttonSet = h_1("div", { style: { float: 'right' } });
                const deleteButton = new OO.ui.ButtonWidget({
                    icon: 'trash',
                    title: mw.msg('deputy.ante.mergedTo.remove'),
                    framed: false,
                    flags: ['destructive']
                });
                deleteButton.on('click', () => {
                    this.mergedToTemplate.destroy();
                });
                buttonSet.appendChild(unwrapWidget(deleteButton));
                return buttonSet;
            }
            /**
             * @return The rendered header of this PageLayout.
             */
            renderHeader() {
                return h_1("h3", null, this.label);
            }
            /**
             * @return The options for this template
             */
            renderTemplateOptions() {
                const layout = new OO.ui.FieldsetLayout({
                    icon: 'parameter',
                    label: mw.msg('deputy.ante.templateOptions'),
                    classes: ['cte-fieldset']
                });
                const rowDate = this.mergedToTemplate.date;
                const parsedDate = (rowDate == null || rowDate.trim().length === 0) ?
                    undefined : (!isNaN(new Date(rowDate.trim() + ' UTC').getTime()) ?
                    (new Date(rowDate.trim() + ' UTC')) : (!isNaN(new Date(rowDate.trim()).getTime()) ?
                    new Date(rowDate.trim()) : null));
                const inputs = {
                    to: SmartTitleInputWidget({
                        $overlay: this.parent.$overlay,
                        required: true,
                        value: this.mergedToTemplate.to || '',
                        placeholder: mw.msg('deputy.ante.mergedTo.to.placeholder')
                    }),
                    date: new mw.widgets.DateInputWidget({
                        $overlay: this.parent.$overlay,
                        required: true,
                        icon: 'calendar',
                        value: parsedDate ? `${parsedDate.getUTCFullYear()}-${parsedDate.getUTCMonth() + 1}-${parsedDate.getUTCDate()}` : undefined,
                        placeholder: mw.msg('deputy.ante.copied.date.placeholder')
                    }),
                    small: new OO.ui.CheckboxInputWidget({
                        selected: yesNo(this.mergedToTemplate.small, false)
                    })
                };
                const fieldLayouts = {
                    to: new OO.ui.FieldLayout(inputs.to, {
                        $overlay: this.parent.$overlay,
                        align: 'top',
                        label: mw.msg('deputy.ante.mergedTo.to.label'),
                        help: mw.msg('deputy.ante.mergedTo.to.help')
                    }),
                    date: new OO.ui.FieldLayout(inputs.date, {
                        $overlay: this.parent.$overlay,
                        align: 'left',
                        label: mw.msg('deputy.ante.mergedTo.date.label'),
                        help: mw.msg('deputy.ante.mergedTo.date.help')
                    }),
                    small: new OO.ui.FieldLayout(inputs.small, {
                        $overlay: this.parent.$overlay,
                        align: 'inline',
                        label: mw.msg('deputy.ante.mergedTo.small.label'),
                        help: mw.msg('deputy.ante.mergedTo.small.help')
                    })
                };
                for (const _field in inputs) {
                    const field = _field;
                    const input = inputs[field];
                    // Attach the change listener
                    input.on('change', (value) => {
                        if (input instanceof OO.ui.CheckboxInputWidget) {
                            this.mergedToTemplate[field] = value ? 'yes' : 'no';
                        }
                        else if (input instanceof mw.widgets.DateInputWidget) {
                            this.mergedToTemplate[field] = value ?
                                window.moment(value, 'YYYY-MM-DD')
                                    .locale(mw.config.get('wgContentLanguage'))
                                    .format('D MMMM Y') : undefined;
                            if (value.length > 0) {
                                fieldLayouts[field].setWarnings([]);
                            }
                        }
                        else {
                            this.mergedToTemplate[field] = value;
                        }
                        this.mergedToTemplate.save();
                    });
                    if (input instanceof OO.ui.TextInputWidget) {
                        // Rechecks the validity of the field.
                        input.setValidityFlag();
                    }
                }
                inputs.to.on('change', () => {
                    this.refreshLabel();
                });
                layout.addItems(getObjectValues(fieldLayouts));
                return unwrapWidget(layout);
            }
            /**
             * Sets up the outline item of this page. Used in the BookletLayout.
             */
            setupOutlineItem() {
                if (this.outlineItem !== undefined) {
                    this.outlineItem
                        .setMovable(true)
                        .setRemovable(true)
                        .setIcon('puzzle')
                        .setLevel(0)
                        .setLabel(this.label);
                }
            }
        };
    }
    /**
     * Creates a new MergedToTemplatePage.
     *
     * @param config Configuration to be passed to the element.
     * @return A MergedToTemplatePage object
     */
    function MergedToTemplatePage (config) {
        if (!InternalMergedToTemplatePage) {
            initMergedToTemplatePage();
        }
        return new InternalMergedToTemplatePage(config);
    }

    /**
     * Represents a single {{merged-to}} template in the Parsoid document.
     */
    class MergedToTemplate extends AttributionNotice {
        /**
         * inheritDoc
         */
        parse() {
            if (this.node.hasParameter('to')) {
                this.to = this.node.getParameter('to');
            }
            else if (this.node.hasParameter('1')) {
                this.to = this.node.getParameter('1');
            }
            if (this.node.hasParameter('date')) {
                this.date = this.node.getParameter('date');
            }
            else if (this.node.hasParameter('2')) {
                this.date = this.node.getParameter('2');
            }
            if (this.node.hasParameter('small')) {
                this.small = this.node.getParameter('small');
            }
        }
        /**
         * @inheritDoc
         */
        save() {
            // Reset named parameters
            this.node.setParameter('to', null);
            this.node.setParameter('date', null);
            this.node.setParameter('1', this.to);
            this.node.setParameter('2', this.date);
            if (this.small !== undefined) {
                this.node.setParameter('small', yesNo(this.small) ? 'yes' : null);
            }
            this.dispatchEvent(new Event('save'));
        }
        /**
         * @inheritDoc
         */
        destroy() {
            this.node.destroy();
            // Self-destruct
            Object.keys(this).forEach((k) => delete this[k]);
            this.dispatchEvent(new Event('destroy'));
        }
        /**
         * @inheritDoc
         */
        generatePage(dialog) {
            return MergedToTemplatePage({
                mergedToTemplate: this,
                parent: dialog
            });
        }
    }

    /**
     * Clones a regular expression.
     *
     * @param regex The regular expression to clone.
     * @param options
     * @return A new regular expression object.
     */
    function cloneRegex$1 (regex, options = {}) {
        return new RegExp(options.transformer ? options.transformer(regex.source) :
            `${options.pre || ''}${regex.source}${options.post || ''}`, regex.flags);
    }

    /**
     * Replacement for String.prototype.matchALl (ES2020 only)
     *
     * @param _regex The regular expression to exec with
     * @param string The string to exec against
     * @return The matches found
     */
    function matchAll(_regex, string) {
        const regex = cloneRegex$1(_regex);
        const res = [];
        let current = regex.exec(string);
        while (current != null) {
            res.push(current);
            current = regex.exec(string);
        }
        return res;
    }

    let InternalBackwardsCopyTemplateRowPage;
    /**
     * The UI representation of a {{copied}} template row. This refers to a set of `diff`, `to`,
     * or `from` parameters on each {{copied}} template.
     *
     * Note that "Page" in the class title does not refer to a MediaWiki page, but rather
     * a OOUI PageLayout.
     */
    function initBackwardsCopyTemplateRowPage() {
        InternalBackwardsCopyTemplateRowPage = class BackwardsCopyTemplateRowPage extends OO.ui.PageLayout {
            /**
             * @param config Configuration to be passed to the element.
             */
            constructor(config) {
                const { backwardsCopyTemplateRow, parent } = config;
                if (parent == null) {
                    throw new Error('Parent dialog (BackwardsCopyTemplateEditorDialog) is required');
                }
                else if (backwardsCopyTemplateRow == null) {
                    throw new Error('Reference row (BackwardsCopyTemplateRow) is required');
                }
                const finalConfig = {
                    classes: ['cte-page-row']
                };
                super(backwardsCopyTemplateRow.id, finalConfig);
                this.parent = parent;
                this.backwardsCopyTemplateRow = backwardsCopyTemplateRow;
                this.refreshLabel();
                this.backwardsCopyTemplateRow.parent.addEventListener('destroy', () => {
                    parent.rebuildPages();
                });
                this.backwardsCopyTemplateRow.parent.addEventListener('rowDelete', () => {
                    parent.rebuildPages();
                });
                this.backwardsCopyTemplateRow.parent.addEventListener('save', () => {
                    this.refreshLabel();
                });
                this.$element.append(this.render().$element);
            }
            /**
             * Refreshes the page's label
             */
            refreshLabel() {
                this.label = mw.message('deputy.ante.backwardsCopy.entry.short', this.backwardsCopyTemplateRow.title || '???').text();
                if (this.outlineItem) {
                    this.outlineItem.setLabel(this.label);
                }
            }
            /**
             * Renders this page. Returns a FieldsetLayout OOUI widget.
             *
             * @return An OOUI FieldsetLayout
             */
            render() {
                this.layout = new OO.ui.FieldsetLayout({
                    icon: 'parameter',
                    label: mw.msg('deputy.ante.copied.entry.label'),
                    classes: ['cte-fieldset']
                });
                this.layout.$element.append(this.renderButtons());
                this.layout.addItems(this.renderFields());
                return this.layout;
            }
            /**
             * Renders a set of buttons used to modify a specific {{copied}} template row.
             *
             * @return An array of OOUI FieldLayouts
             */
            renderButtons() {
                const deleteButton = new OO.ui.ButtonWidget({
                    icon: 'trash',
                    title: mw.msg('deputy.ante.backwardsCopy.entry.remove'),
                    framed: false,
                    flags: ['destructive']
                });
                deleteButton.on('click', () => {
                    this.backwardsCopyTemplateRow.parent.deleteRow(this.backwardsCopyTemplateRow);
                });
                return h_1("div", { style: {
                        float: 'right',
                        position: 'absolute',
                        top: '0.5em',
                        right: '0.5em'
                    } }, unwrapWidget(deleteButton));
            }
            /**
             * Renders a set of OOUI InputWidgets and FieldLayouts, eventually returning an
             * array of each FieldLayout to append to the FieldsetLayout.
             *
             * @return An array of OOUI FieldLayouts
             */
            renderFields() {
                var _a, _b, _c, _d;
                // Use order: `date`, `monthday` + `year`, `year`
                const rowDate = (_a = this.backwardsCopyTemplateRow.date) !== null && _a !== void 0 ? _a : (this.backwardsCopyTemplateRow.monthday ?
                    `${this.backwardsCopyTemplateRow.monthday} ${this.backwardsCopyTemplateRow.year}` :
                    this.backwardsCopyTemplateRow.year);
                // TODO: ANTE l10n
                const authorRegex = /(.+?, (?:[A-Z]\.\s?)*)(?:(?:&amp;|[&;]|[,;] (?:&amp;|[&;])?)\s*|$)/g;
                const authors = matchAll(authorRegex, (_b = this.backwardsCopyTemplateRow.authorlist) !== null && _b !== void 0 ? _b : this.backwardsCopyTemplateRow.author).map((v) => v[1]);
                const inputs = {
                    title: new OO.ui.TextInputWidget({
                        required: true,
                        placeholder: mw.msg('deputy.ante.backwardsCopy.entry.title.placeholder'),
                        value: (_c = this.backwardsCopyTemplateRow.title) !== null && _c !== void 0 ? _c : this.backwardsCopyTemplateRow.articlename
                    }),
                    date: new OO.ui.TextInputWidget({
                        placeholder: mw.msg('deputy.ante.backwardsCopy.entry.date.placeholder'),
                        value: rowDate
                    }),
                    author: new OO.ui.TextInputWidget({
                        placeholder: mw.msg('deputy.ante.backwardsCopy.entry.author.placeholder'),
                        value: (_d = authors[0]) !== null && _d !== void 0 ? _d : this.backwardsCopyTemplateRow.author
                    }),
                    url: new OO.ui.TextInputWidget({
                        placeholder: mw.msg('deputy.ante.backwardsCopy.entry.url.placeholder'),
                        value: this.backwardsCopyTemplateRow.url,
                        validate: (value) => {
                            if (value.trim().length === 0) {
                                return true;
                            }
                            try {
                                return typeof new URL(value).href === 'string';
                            }
                            catch (e) {
                                return false;
                            }
                        }
                    }),
                    org: new OO.ui.TextInputWidget({
                        placeholder: mw.msg('deputy.ante.backwardsCopy.entry.org.placeholder'),
                        value: this.backwardsCopyTemplateRow.org
                    })
                };
                const fields = {
                    title: new OO.ui.FieldLayout(inputs.title, {
                        $overlay: this.parent.$overlay,
                        label: mw.msg('deputy.ante.backwardsCopy.entry.title.label'),
                        align: 'top',
                        help: mw.msg('deputy.ante.backwardsCopy.entry.title.help')
                    }),
                    date: new OO.ui.FieldLayout(inputs.date, {
                        $overlay: this.parent.$overlay,
                        label: mw.msg('deputy.ante.backwardsCopy.entry.date.label'),
                        align: 'left',
                        help: mw.msg('deputy.ante.backwardsCopy.entry.date.help')
                    }),
                    author: new OO.ui.FieldLayout(inputs.author, {
                        $overlay: this.parent.$overlay,
                        label: mw.msg('deputy.ante.backwardsCopy.entry.author.label'),
                        align: 'left',
                        help: mw.msg('deputy.ante.backwardsCopy.entry.author.help')
                    }),
                    url: new OO.ui.FieldLayout(inputs.url, {
                        $overlay: this.parent.$overlay,
                        label: mw.msg('deputy.ante.backwardsCopy.entry.url.label'),
                        align: 'left',
                        help: mw.msg('deputy.ante.backwardsCopy.entry.url.help')
                    }),
                    org: new OO.ui.FieldLayout(inputs.org, {
                        $overlay: this.parent.$overlay,
                        label: mw.msg('deputy.ante.backwardsCopy.entry.org.label'),
                        align: 'left',
                        help: mw.msg('deputy.ante.backwardsCopy.entry.org.help')
                    })
                };
                for (const _field in inputs) {
                    const field = _field;
                    const input = inputs[field];
                    input.on('change', (value) => {
                        this.backwardsCopyTemplateRow[field] = value;
                        this.backwardsCopyTemplateRow.parent.save();
                    });
                    if (input instanceof OO.ui.TextInputWidget) {
                        // Rechecks the validity of the field.
                        input.setValidityFlag();
                    }
                }
                return getObjectValues(fields);
            }
            /**
             * Sets up the outline item of this page. Used in the BookletLayout.
             */
            setupOutlineItem() {
                if (this.outlineItem !== undefined) {
                    this.outlineItem
                        .setMovable(true)
                        .setRemovable(true)
                        .setIcon('parameter')
                        .setLevel(1)
                        .setLabel(this.label);
                }
            }
        };
    }
    /**
     * Creates a new BackwardsCopyTemplateRowPage.
     *
     * @param config Configuration to be passed to the element.
     * @return A BackwardsCopyTemplateRowPage object
     */
    function BackwardsCopyTemplateRowPage (config) {
        if (!InternalBackwardsCopyTemplateRowPage) {
            initBackwardsCopyTemplateRowPage();
        }
        return new InternalBackwardsCopyTemplateRowPage(config);
    }

    const backwardsCopyTemplateRowParameters = [
        'title', 'year', 'author', 'authorlist',
        'display_authors', 'url', 'org', 'monthday',
        'articlename', 'date'
    ];
    /**
     * Represents a row/entry in a {{copied}} template.
     */
    class BackwardsCopyTemplateRow extends AttributionNoticeRow {
        // noinspection JSDeprecatedSymbols
        /**
         * Creates a new RawBackwardsCopyRow
         *
         * @param rowObjects
         * @param parent
         */
        constructor(rowObjects, parent) {
            super(parent);
            this.articlename = rowObjects.articlename;
            this.title = rowObjects.title;
            this.year = rowObjects.year;
            this.author = rowObjects.author;
            this.authorlist = rowObjects.authorlist;
            // eslint-disable-next-line camelcase
            this.display_authors = rowObjects.display_authors;
            this.url = rowObjects.url;
            this.org = rowObjects.org;
            this.date = rowObjects.date;
            this.monthday = rowObjects.monthday;
        }
        /**
         * @inheritDoc
         */
        clone(parent) {
            return super.clone(parent);
        }
        /**
         * @inheritDoc
         */
        generatePage(dialog) {
            return BackwardsCopyTemplateRowPage({
                backwardsCopyTemplateRow: this,
                parent: dialog
            });
        }
    }

    /**
     * Swaps two elements in the DOM. Element 1 will be removed from the DOM, Element 2 will
     * be added in its place.
     *
     * @param element1 The element to remove
     * @param element2 The element to insert
     * @return `element2`, for chaining
     */
    function swapElements (element1, element2) {
        try {
            element1.insertAdjacentElement('afterend', element2);
            element1.parentElement.removeChild(element1);
            return element2;
        }
        catch (e) {
            error(e, { element1, element2 });
            // Caught for debug only. Rethrow.
            throw e;
        }
    }

    /**
     * Displayed when the actively-edited notice is in a demonstration mode or `nocat` mode.
     *
     * @param nocat
     * @return HTML element
     */
    function DemoTemplateMessage (nocat = false) {
        return h_1("span", null,
            h_1("b", null, mw.message(nocat ? 'deputy.ante.nocat.head' : 'deputy.ante.demo.head').parseDom().get()),
            h_1("br", null),
            mw.message(nocat ? 'deputy.ante.nocat.help' : 'deputy.ante.demo.help').parseDom().get(),
            h_1("br", null),
            h_1("span", { class: "cte-message-button" }));
    }

    /**
     * Removes an element from its document.
     *
     * @param element
     * @return The removed element
     */
    function removeElement (element) {
        var _a;
        return (_a = element === null || element === void 0 ? void 0 : element.parentElement) === null || _a === void 0 ? void 0 : _a.removeChild(element);
    }

    let InternalDeputyMessageWidget;
    /**
     * Initializes the process element.
     */
    function initDeputyMessageWidget() {
        InternalDeputyMessageWidget = class DeputyMessageWidget extends OO.ui.MessageWidget {
            /**
             * @param config Configuration to be passed to the element.
             */
            constructor(config) {
                var _a;
                super(config);
                this.$element.addClass('dp-messageWidget');
                const elLabel = this.$label[0];
                if (!config.label) {
                    if (config.title) {
                        elLabel.appendChild(h_1("b", { style: { display: 'block' } }, config.title));
                    }
                    if (config.message) {
                        elLabel.appendChild(h_1("p", { class: "dp-messageWidget-message" }, config.message));
                    }
                }
                if (config.actions || config.closable) {
                    const actionContainer = h_1("div", { class: "dp-messageWidget-actions" });
                    for (const action of ((_a = config.actions) !== null && _a !== void 0 ? _a : [])) {
                        if (action instanceof OO.ui.Element) {
                            actionContainer.appendChild(unwrapWidget(action));
                        }
                        else {
                            actionContainer.appendChild(action);
                        }
                    }
                    if (config.closable) {
                        const closeButton = new OO.ui.ButtonWidget({
                            label: mw.msg('deputy.dismiss')
                        });
                        closeButton.on('click', () => {
                            removeElement(unwrapWidget(this));
                            this.emit('close');
                        });
                        actionContainer.appendChild(unwrapWidget(closeButton));
                    }
                    elLabel.appendChild(actionContainer);
                }
            }
        };
    }
    /**
     * Creates a new DeputyMessageWidget. This is an extension of the default
     * OOUI MessageWidget. It includes support for a title, a message, and button
     * actions.
     *
     * @param config Configuration to be passed to the element.
     * @return A DeputyMessageWidget object
     */
    function DeputyMessageWidget (config) {
        if (!InternalDeputyMessageWidget) {
            initDeputyMessageWidget();
        }
        return new InternalDeputyMessageWidget(config);
    }

    let InternalBackwardsCopyTemplatePage;
    /**
     * UI representation of a {{backwards copy}} template. This representation is further broken
     * down with `BackwardsCopyTemplateRowPage`, which represents each row on the template.
     *
     * Note that "Page" in the class title does not refer to a MediaWiki page, but rather
     * a OOUI PageLayout.
     */
    function initBackwardsCopyTemplatePage() {
        InternalBackwardsCopyTemplatePage = class BackwardsCopyTemplatePage extends OO.ui.PageLayout {
            /**
             * @param config Configuration to be passed to the element.
             */
            constructor(config) {
                const { backwardsCopyTemplate, parent } = config;
                if (parent == null) {
                    throw new Error('Parent dialog (BackwardsCopyTemplateEditorDialog) is required');
                }
                else if (backwardsCopyTemplate == null) {
                    throw new Error('Reference template (BackwardsCopyTemplate) is required');
                }
                const label = mw.message('deputy.ante.backwardsCopy.label', config.backwardsCopyTemplate.name).text();
                const finalConfig = {
                    label: label,
                    classes: ['cte-page-template']
                };
                super(backwardsCopyTemplate.id, finalConfig);
                /**
                 * All child pages of this BackwardsCopyTemplatePage. Garbage collected when rechecked.
                 */
                this.childPages = new Map();
                this.document = config.backwardsCopyTemplate.parsoid;
                this.backwardsCopyTemplate = config.backwardsCopyTemplate;
                this.parent = config.parent;
                this.label = label;
                backwardsCopyTemplate.addEventListener('rowAdd', () => {
                    parent.rebuildPages();
                });
                backwardsCopyTemplate.addEventListener('rowDelete', () => {
                    parent.rebuildPages();
                });
                backwardsCopyTemplate.addEventListener('destroy', () => {
                    parent.rebuildPages();
                });
                this.$element.append(this.renderButtons(), this.renderHeader(), renderMergePanel('backwardsCopy', this.backwardsCopyTemplate, this.mergeButton), this.renderBotPanel(), this.renderDemoPanel(), renderPreviewPanel(this.backwardsCopyTemplate), this.renderTemplateOptions());
            }
            /**
             * @inheritDoc
             */
            getChildren() {
                const rows = this.backwardsCopyTemplate.rows;
                const rowPages = [];
                for (const row of rows) {
                    if (!this.childPages.has(row)) {
                        this.childPages.set(row, row.generatePage(this.parent));
                    }
                    rowPages.push(this.childPages.get(row));
                }
                // Delete deleted rows from cache.
                this.childPages.forEach((page, row) => {
                    if (rowPages.indexOf(page) === -1) {
                        this.childPages.delete(row);
                    }
                });
                return rowPages;
            }
            /**
             * @return The rendered header of this PageLayout.
             */
            renderHeader() {
                return h_1("h3", null, this.label);
            }
            /**
             * Renders the set of buttons that appear at the top of the page.
             *
             * @return A <div> element.
             */
            renderButtons() {
                const buttonSet = h_1("div", { style: { float: 'right' } });
                this.mergeButton = new OO.ui.ButtonWidget({
                    icon: 'tableMergeCells',
                    title: mw.msg('deputy.ante.merge'),
                    framed: false
                });
                const deleteButton = new OO.ui.ButtonWidget({
                    icon: 'trash',
                    title: mw.msg('deputy.ante.copied.remove'),
                    framed: false,
                    flags: ['destructive']
                });
                deleteButton.on('click', () => {
                    if (this.backwardsCopyTemplate.rows.length > 0) {
                        dangerModeConfirm(window.CopiedTemplateEditor.config, mw.message('deputy.ante.copied.remove.confirm', `${this.backwardsCopyTemplate.rows.length}`).text()).done((confirmed) => {
                            if (confirmed) {
                                this.backwardsCopyTemplate.destroy();
                            }
                        });
                    }
                    else {
                        this.backwardsCopyTemplate.destroy();
                    }
                });
                const addButton = new OO.ui.ButtonWidget({
                    flags: ['progressive'],
                    icon: 'add',
                    label: mw.msg('deputy.ante.copied.add')
                });
                addButton.on('click', () => {
                    this.backwardsCopyTemplate.addRow(new BackwardsCopyTemplateRow({}, this.backwardsCopyTemplate));
                });
                buttonSet.appendChild(unwrapWidget(this.mergeButton));
                buttonSet.appendChild(unwrapWidget(deleteButton));
                buttonSet.appendChild(unwrapWidget(addButton));
                return buttonSet;
            }
            /**
             * Renders a panel that shows when a bot is used.
             *
             * @return An unwrapped OOUI MessageWidget
             */
            renderBotPanel() {
                if (this.backwardsCopyTemplate.node.hasParameter('bot')) {
                    const bot = this.backwardsCopyTemplate.node.getParameter('bot');
                    return unwrapWidget(DeputyMessageWidget({
                        type: 'notice',
                        icon: 'robot',
                        label: new OO.ui.HtmlSnippet(mw.message('deputy.ante.backwardsCopy.bot', bot).parse()),
                        closable: true
                    }));
                }
                else {
                    return null;
                }
            }
            /**
             * Renders a panel that shows when demo mode is enabled.
             *
             * @return An unwrapped OOUI MessageWidget
             */
            renderDemoPanel() {
                if (this.backwardsCopyTemplate.node.hasParameter('bot')) {
                    // Insert element directly into widget (not as text, or else event
                    // handlers will be destroyed).
                    const messageBox = DeputyMessageWidget({
                        type: 'notice',
                        icon: 'alert',
                        label: new OO.ui.HtmlSnippet(DemoTemplateMessage().innerHTML),
                        closable: true
                    });
                    const clearButton = new OO.ui.ButtonWidget({
                        flags: ['progressive', 'primary'],
                        label: mw.msg('deputy.ante.demo.clear')
                    });
                    clearButton.on('click', () => {
                        this.backwardsCopyTemplate.node.removeParameter('demo');
                        removeElement(unwrapWidget(messageBox));
                    });
                    swapElements(unwrapWidget(messageBox)
                        .querySelector('.cte-message-button'), unwrapWidget(clearButton));
                    return unwrapWidget(messageBox);
                }
                else {
                    return null;
                }
            }
            /**
             * Renders the panel used to merge multiple {{copied}} templates.
             *
             * @return A <div> element
             */
            renderMergePanel() {
                return renderMergePanel('backwardsCopy', this.backwardsCopyTemplate, this.mergeButton);
            }
            /**
             * Renders the global options of this template. This includes parameters that are not
             * counted towards an entry and affect the template as a whole.
             *
             * @return A <div> element.
             */
            renderTemplateOptions() {
                var _a, _b;
                const inputSet = {
                    comments: new OO.ui.TextInputWidget({
                        placeholder: mw.msg('deputy.ante.backwardsCopy.comments.placeholder'),
                        value: (_a = this.backwardsCopyTemplate.comments) === null || _a === void 0 ? void 0 : _a.trim()
                    }),
                    id: new OO.ui.TextInputWidget({
                        placeholder: mw.msg('deputy.ante.backwardsCopy.id.placeholder'),
                        value: (_b = this.backwardsCopyTemplate.revid) === null || _b === void 0 ? void 0 : _b.trim()
                    })
                };
                const fields = {
                    comments: new OO.ui.FieldLayout(inputSet.comments, {
                        $overlay: this.parent.$overlay,
                        label: mw.msg('deputy.ante.backwardsCopy.comments.label'),
                        help: mw.msg('deputy.ante.backwardsCopy.comments.help'),
                        align: 'top'
                    }),
                    id: new OO.ui.FieldLayout(inputSet.id, {
                        $overlay: this.parent.$overlay,
                        label: mw.msg('deputy.ante.backwardsCopy.id.label'),
                        help: mw.msg('deputy.ante.backwardsCopy.id.help'),
                        align: 'top'
                    })
                };
                inputSet.comments.on('change', (value) => {
                    this.backwardsCopyTemplate.comments = value.trim();
                    this.backwardsCopyTemplate.save();
                });
                inputSet.id.on('change', (value) => {
                    this.backwardsCopyTemplate.revid = value.trim();
                    this.backwardsCopyTemplate.save();
                });
                return h_1("div", { class: "cte-templateOptions" },
                    h_1("div", { style: { marginRight: '8px' } }, unwrapWidget(fields.comments)),
                    h_1("div", { style: { flex: '0.5' } }, unwrapWidget(fields.id)));
            }
            /**
             * Sets up the outline item of this page. Used in the BookletLayout.
             */
            setupOutlineItem() {
                if (this.outlineItem !== undefined) {
                    /** @member any */
                    this.outlineItem
                        .setMovable(true)
                        .setRemovable(true)
                        .setIcon('puzzle')
                        .setLevel(0)
                        .setLabel(this.label);
                }
            }
        };
    }
    /**
     * Creates a new BackwardsCopyTemplatePage.
     *
     * @param config Configuration to be passed to the element.
     * @return A BackwardsCopyTemplatePage object
     */
    function BackwardsCopyTemplatePage (config) {
        if (!InternalBackwardsCopyTemplatePage) {
            initBackwardsCopyTemplatePage();
        }
        return new InternalBackwardsCopyTemplatePage(config);
    }

    /**
     * Represents a single {{copied}} template in the Parsoid document.
     */
    class BackwardsCopyTemplate extends RowedAttributionNotice {
        /**
         * @return This template's rows.
         */
        get rows() {
            return this._rows;
        }
        /**
         * Parses parameters into class properties. This WILL destroy unknown
         * parameters and parameters in the incorrect order!
         *
         * This function does not modify the template data.
         */
        parse() {
            if (this.node.getParameter('demo')) {
                this.demo = this.node.getParameter('demo');
            }
            if (this.node.getParameter('comments')) {
                this.comments = this.node.getParameter('comments');
            }
            if (this.node.getParameter('id')) {
                this.revid = this.node.getParameter('id');
            }
            // Extract {{backwards copy}} rows.
            const rows = [];
            // Numberless
            if (this.hasRowParameters(backwardsCopyTemplateRowParameters)) {
                // If `from`, `to`, ..., or `merge` is found.
                rows.push(new BackwardsCopyTemplateRow(this.extractRowParameters(backwardsCopyTemplateRowParameters), this));
            }
            // Numbered
            let i = 1, continueExtracting = true;
            do {
                if (this.hasRowParameters(backwardsCopyTemplateRowParameters, i)) {
                    rows.push(new BackwardsCopyTemplateRow(this.extractRowParameters(backwardsCopyTemplateRowParameters, i), this));
                }
                else if (!(i === 1 && rows.length > 0)) {
                    // Row doesn't exist. Stop parsing from here.
                    continueExtracting = false;
                }
                i++;
            } while (continueExtracting);
            /**
             * All the rows of this template.
             *
             * @type {BackwardsCopyTemplateRow[]}
             */
            this._rows = rows;
        }
        /**
         * Saves the current template data to the Parsoid element.
         */
        save() {
            this.node.removeParameter('bot');
            if (this.demo) {
                this.node.setParameter('demo', this.demo);
            }
            this.node.setParameter('comments', this.comments);
            this.node.setParameter('id', this.revid);
            const existingParameters = this.node.getParameters();
            for (const param in existingParameters) {
                if (backwardsCopyTemplateRowParameters.some((v) => param.startsWith(v))) {
                    // This is a row parameter. Remove it in preparation for rebuild (further below).
                    this.node.removeParameter(param);
                }
            }
            if (this._rows.length === 1) {
                // If there is only one row, don't bother with numbered rows.
                for (const param of backwardsCopyTemplateRowParameters) {
                    if (this._rows[0][param] !== undefined) {
                        this.node.setParameter(param, this._rows[0][param]);
                    }
                }
            }
            else {
                // If there are multiple rows, add number suffixes (except for i = 0).
                for (let i = 0; i < this._rows.length; i++) {
                    for (const param of backwardsCopyTemplateRowParameters) {
                        if (this._rows[i][param] !== undefined) {
                            this.node.setParameter(param + (i === 0 ? '' : i + 1), this._rows[i][param]);
                        }
                    }
                }
            }
            this.dispatchEvent(new Event('save'));
        }
        /**
         * Destroys this template completely.
         */
        destroy() {
            this.node.destroy();
            // Self-destruct
            Object.keys(this).forEach((k) => delete this[k]);
            this.dispatchEvent(new Event('destroy'));
        }
        /**
         * @inheritDoc
         */
        generatePage(dialog) {
            return BackwardsCopyTemplatePage({
                backwardsCopyTemplate: this,
                parent: dialog
            });
        }
        /**
         * Copies in the rows of another {@link BackwardsCopyTemplate}, and
         * optionally deletes that template or clears its contents.
         *
         * @param template The template to copy from.
         * @param options Options for this merge.
         * @param options.delete
         *        Whether the reference template will be deleted after merging.
         * @param options.clear
         *        Whether the reference template's rows will be cleared after merging.
         */
        merge(template, options = {}) {
            if (template.rows === undefined || template === this) {
                // Deleted or self
                return;
            }
            for (const row of template.rows) {
                if (options.clear) {
                    row.parent = this;
                }
                else {
                    this.addRow(row.clone(this));
                }
            }
            if (options.delete) {
                template.destroy();
            }
        }
    }

    let InternalTranslatedPageTemplatePage;
    /**
     * Initializes the process element.
     */
    function initTranslatedPageTemplatePage() {
        InternalTranslatedPageTemplatePage = class TranslatedPageTemplatePage extends OO.ui.PageLayout {
            /**
             * @param config Configuration to be passed to the element.
             */
            constructor(config) {
                const { translatedPageTemplate, parent } = config;
                if (parent == null) {
                    throw new Error('Parent dialog (CopiedTemplateEditorDialog) is required');
                }
                else if (translatedPageTemplate == null) {
                    throw new Error('Reference template (TranslatedPageTemplate) is required');
                }
                const finalConfig = {
                    classes: ['cte-page-template']
                };
                super(translatedPageTemplate.id, finalConfig);
                this.document = translatedPageTemplate.parsoid;
                this.translatedPageTemplate = translatedPageTemplate;
                this.parent = config.parent;
                this.refreshLabel();
                translatedPageTemplate.addEventListener('destroy', () => {
                    parent.rebuildPages();
                });
                this.$element.append(this.renderButtons(), this.renderHeader(), renderPreviewPanel(this.translatedPageTemplate), this.renderTemplateOptions());
            }
            /**
             * Refreshes the page's label
             */
            refreshLabel() {
                this.label = mw.message('deputy.ante.translatedPage.label', this.translatedPageTemplate.lang || '??', this.translatedPageTemplate.page || '???').text();
                if (this.outlineItem) {
                    this.outlineItem.setLabel(this.label);
                }
            }
            /**
             * Renders the set of buttons that appear at the left of the page.
             *
             * @return A <div> element.
             */
            renderButtons() {
                const copyButton = new OO.ui.ButtonWidget({
                    icon: 'quotes',
                    title: mw.msg('deputy.ante.translatedPage.copy'),
                    framed: false
                });
                copyButton.on('click', () => {
                    // TODO: Find out a way to l10n-ize this.
                    let attributionString = `[[WP:PATT|Attribution]]: Content translated from [[:${this.translatedPageTemplate.lang}:`;
                    let lacking = false;
                    if (this.translatedPageTemplate.page != null &&
                        this.translatedPageTemplate.page.length !== 0) {
                        attributionString += `${this.translatedPageTemplate.page}]]`;
                    }
                    else {
                        lacking = true;
                        if (this.translatedPageTemplate.version != null) {
                            attributionString += `|from a page on ${this.translatedPageTemplate.lang}.wikipedia]]`;
                        }
                    }
                    if (this.translatedPageTemplate.version != null) {
                        attributionString += ` as of revision [[:${this.translatedPageTemplate.lang}:Special:Diff/${this.translatedPageTemplate.version}|${this.translatedPageTemplate.version}]]`;
                    }
                    if (this.translatedPageTemplate.insertversion != null &&
                        this.translatedPageTemplate.insertversion.length !== 0) {
                        attributionString += ` with [[Special:Diff/${this.translatedPageTemplate.insertversion}|this edit]] et seq.`;
                    }
                    if (this.translatedPageTemplate.page != null &&
                        this.translatedPageTemplate.page.length !== 0) {
                        attributionString += `; refer to that page's [[:${this.translatedPageTemplate.lang}:Special:PageHistory/${this.translatedPageTemplate.page}|edit history]] for additional attribution`;
                    }
                    attributionString += '.';
                    copyToClipboard(attributionString);
                    if (lacking) {
                        mw.notify(mw.msg('deputy.ante.translatedPage.copy.lacking'), { title: mw.msg('deputy.ante'), type: 'warn' });
                    }
                    else {
                        mw.notify(mw.msg('deputy.ante.translatedPage.copy.success'), { title: mw.msg('deputy.ante') });
                    }
                });
                const deleteButton = new OO.ui.ButtonWidget({
                    icon: 'trash',
                    title: mw.msg('deputy.ante.translatedPage.remove'),
                    framed: false,
                    flags: ['destructive']
                });
                deleteButton.on('click', () => {
                    this.translatedPageTemplate.destroy();
                });
                return h_1("div", { style: { float: 'right' } },
                    unwrapWidget(copyButton),
                    unwrapWidget(deleteButton));
            }
            /**
             * @return The rendered header of this PageLayout.
             */
            renderHeader() {
                return h_1("h3", null, this.label);
            }
            /**
             * @return The options for this template
             */
            renderTemplateOptions() {
                var _a;
                const layout = new OO.ui.FieldsetLayout({
                    icon: 'parameter',
                    label: mw.msg('deputy.ante.templateOptions'),
                    classes: ['cte-fieldset']
                });
                const searchApi = new mw.ForeignApi(mw.util.wikiScript('api'), {
                    anonymous: true
                });
                const inputs = {
                    lang: new OO.ui.TextInputWidget({
                        required: true,
                        value: this.translatedPageTemplate.lang,
                        placeholder: mw.msg('deputy.ante.translatedPage.lang.placeholder'),
                        validate: /^[a-z\d-]+$/gi
                    }),
                    page: new mw.widgets.TitleInputWidget({
                        $overlay: this.parent.$overlay,
                        api: searchApi,
                        required: true,
                        value: this.translatedPageTemplate.page || '',
                        placeholder: mw.msg('deputy.ante.translatedPage.page.placeholder')
                    }),
                    comments: new OO.ui.TextInputWidget({
                        value: this.translatedPageTemplate.comments,
                        placeholder: mw.msg('deputy.ante.translatedPage.comments.placeholder')
                    }),
                    version: new OO.ui.TextInputWidget({
                        value: this.translatedPageTemplate.version,
                        placeholder: mw.msg('deputy.ante.translatedPage.version.placeholder'),
                        validate: /^\d+$/gi
                    }),
                    insertversion: new OO.ui.TextInputWidget({
                        value: this.translatedPageTemplate.insertversion,
                        placeholder: mw.msg('deputy.ante.translatedPage.insertversion.placeholder'),
                        validate: /^[\d/]+$/gi
                    }),
                    section: new OO.ui.TextInputWidget({
                        value: this.translatedPageTemplate.section,
                        placeholder: mw.msg('deputy.ante.translatedPage.section.placeholder')
                    }),
                    small: new OO.ui.CheckboxInputWidget({
                        selected: yesNo((_a = this.translatedPageTemplate.small) !== null && _a !== void 0 ? _a : 'yes')
                    }),
                    partial: new OO.ui.CheckboxInputWidget({
                        selected: !!this.translatedPageTemplate.partial
                    })
                };
                const fieldLayouts = {
                    lang: new OO.ui.FieldLayout(inputs.lang, {
                        $overlay: this.parent.$overlay,
                        align: 'left',
                        label: mw.msg('deputy.ante.translatedPage.lang.label'),
                        help: mw.msg('deputy.ante.translatedPage.lang.help')
                    }),
                    page: new OO.ui.FieldLayout(inputs.page, {
                        $overlay: this.parent.$overlay,
                        align: 'left',
                        label: mw.msg('deputy.ante.translatedPage.page.label'),
                        help: mw.msg('deputy.ante.translatedPage.page.help')
                    }),
                    comments: new OO.ui.FieldLayout(inputs.comments, {
                        $overlay: this.parent.$overlay,
                        align: 'left',
                        label: mw.msg('deputy.ante.translatedPage.comments.label'),
                        help: mw.msg('deputy.ante.translatedPage.comments.help')
                    }),
                    version: new OO.ui.FieldLayout(inputs.version, {
                        $overlay: this.parent.$overlay,
                        align: 'left',
                        label: mw.msg('deputy.ante.translatedPage.version.label'),
                        help: mw.msg('deputy.ante.translatedPage.version.help')
                    }),
                    insertversion: new OO.ui.FieldLayout(inputs.insertversion, {
                        $overlay: this.parent.$overlay,
                        align: 'left',
                        label: mw.msg('deputy.ante.translatedPage.insertversion.label'),
                        help: mw.msg('deputy.ante.translatedPage.insertversion.help')
                    }),
                    section: new OO.ui.FieldLayout(inputs.section, {
                        $overlay: this.parent.$overlay,
                        align: 'left',
                        label: mw.msg('deputy.ante.translatedPage.section.label'),
                        help: mw.msg('deputy.ante.translatedPage.section.help')
                    }),
                    small: new OO.ui.FieldLayout(inputs.small, {
                        $overlay: this.parent.$overlay,
                        align: 'inline',
                        label: mw.msg('deputy.ante.translatedPage.small.label'),
                        help: mw.msg('deputy.ante.translatedPage.small.help')
                    }),
                    partial: new OO.ui.FieldLayout(inputs.partial, {
                        $overlay: this.parent.$overlay,
                        align: 'inline',
                        label: mw.msg('deputy.ante.translatedPage.partial.label'),
                        help: mw.msg('deputy.ante.translatedPage.partial.help')
                    })
                };
                for (const _field in inputs) {
                    const field = _field;
                    const input = inputs[field];
                    // Attach the change listener
                    input.on('change', (value) => {
                        if (input instanceof OO.ui.CheckboxInputWidget) {
                            this.translatedPageTemplate[field] = value ? 'yes' : 'no';
                        }
                        else {
                            this.translatedPageTemplate[field] =
                                typeof value === 'string' ? value.trim() : value;
                        }
                        this.translatedPageTemplate.save();
                    });
                    if (input instanceof OO.ui.TextInputWidget) {
                        // Rechecks the validity of the field.
                        input.setValidityFlag();
                    }
                }
                inputs.lang.on('change', (value) => {
                    this.refreshLabel();
                    if (!/^[a-z\d-]+$/gi.test(value)) {
                        return;
                    }
                    searchApi.apiUrl = searchApi.defaults.ajax.url =
                        '//' + value + '.wikipedia.org/w/api.php';
                });
                inputs.page.on('change', () => {
                    this.refreshLabel();
                });
                if (this.translatedPageTemplate.lang) {
                    searchApi.apiUrl = searchApi.defaults.ajax.url =
                        '//' + this.translatedPageTemplate.lang + '.wikipedia.org/w/api.php';
                }
                layout.addItems(getObjectValues(fieldLayouts));
                return unwrapWidget(layout);
            }
            /**
             * Sets up the outline item of this page. Used in the BookletLayout.
             */
            setupOutlineItem() {
                if (this.outlineItem !== undefined) {
                    this.outlineItem
                        .setMovable(true)
                        .setRemovable(true)
                        .setIcon('puzzle')
                        .setLevel(0)
                        .setLabel(this.label);
                }
            }
        };
    }
    /**
     * Creates a new TranslatedPageTemplatePage.
     *
     * @param config Configuration to be passed to the element.
     * @return A TranslatedPageTemplatePage object
     */
    function TranslatedPageTemplatePage (config) {
        if (!InternalTranslatedPageTemplatePage) {
            initTranslatedPageTemplatePage();
        }
        return new InternalTranslatedPageTemplatePage(config);
    }

    /**
     * Represents a single {{merged-from}} template in the Parsoid document.
     */
    class TranslatedPageTemplate extends AttributionNotice {
        /**
         * @inheritDoc
         */
        parse() {
            this.lang = this.node.getParameter('1');
            this.page = this.node.getParameter('2');
            this.comments = this.node.getParameter('3');
            this.version = this.node.getParameter('version');
            this.insertversion = this.node.getParameter('insertversion');
            this.section = this.node.getParameter('section');
            this.small = this.node.getParameter('small');
            this.partial = this.node.getParameter('partial');
        }
        /**
         * @inheritDoc
         */
        save() {
            this.node.setParameter('1', this.lang);
            this.node.setParameter('2', this.page);
            this.node.setParameter('3', this.comments);
            this.node.setParameter('version', this.version);
            this.node.setParameter('insertversion', this.insertversion);
            this.node.setParameter('section', this.section);
            if (this.small !== undefined) {
                this.node.setParameter('small', yesNo(this.small) ? null : 'no');
            }
            if (this.partial !== undefined) {
                this.node.setParameter('partial', yesNo(this.partial) ? 'yes' : null);
            }
            this.dispatchEvent(new Event('save'));
        }
        /**
         * @inheritDoc
         */
        destroy() {
            this.node.destroy();
            // Self-destruct
            Object.keys(this).forEach((k) => delete this[k]);
            this.dispatchEvent(new Event('destroy'));
        }
        /**
         * @inheritDoc
         */
        generatePage(dialog) {
            return TranslatedPageTemplatePage({
                translatedPageTemplate: this,
                parent: dialog
            });
        }
    }

    /**
     * An object mapping notice types to their expected on-wiki page titles.
     */
    const attributionNoticeTemplatePages = {
        copied: 'Copied',
        splitArticle: 'Split article',
        mergedFrom: 'Merged-from',
        mergedTo: 'Merged-to',
        backwardsCopy: 'Backwards copy',
        translatedPage: 'Translated page'
    };
    /**
     * This class contains functions, utilities, and other variables that assist in connecting
     * attribution notice templates on-wiki and converting them into their AttributionNotice
     * counterparts.
     */
    class WikiAttributionNotices {
        /**
         * Initializes.
         */
        static init() {
            return __awaiter(this, void 0, void 0, function* () {
                const attributionNoticeTemplates = {};
                const templateAliasCache = {};
                for (const key of Object.keys(attributionNoticeTemplatePages)) {
                    attributionNoticeTemplates[key] = new mw.Title(attributionNoticeTemplatePages[key], nsId('template'));
                    templateAliasCache[key] = [attributionNoticeTemplates[key]];
                }
                this.attributionNoticeTemplates = attributionNoticeTemplates;
                this.templateAliasCache = templateAliasCache;
                // templateAliasCache setup
                const aliasRequest = yield MwApi.action.get({
                    action: 'query',
                    format: 'json',
                    prop: 'linkshere',
                    titles: getObjectValues(this.attributionNoticeTemplates)
                        .map((v) => v.getPrefixedText())
                        .join('|'),
                    lhprop: 'title',
                    lhnamespace: nsId('template'),
                    lhshow: 'redirect',
                    lhlimit: '500'
                });
                const aliasRequestRedirects = toRedirectsObject(aliasRequest.query.redirects);
                for (const page of aliasRequest.query.pages) {
                    let cacheKey;
                    // Find the key of this page in the list of attribution notice templates.
                    // Slightly expensive, but this init should only be run once anyway.
                    for (const key in this.attributionNoticeTemplates) {
                        const templatePage = this.attributionNoticeTemplates[key].getPrefixedText();
                        if (
                        // Page is a perfect match.
                        templatePage === page.title ||
                            // If the page was moved, and the page originally listed above is a redirect.
                            // This checks if the resolved redirect matches the input page.
                            aliasRequestRedirects[templatePage] === page.title) {
                            cacheKey = key;
                            break;
                        }
                    }
                    if (!cacheKey) {
                        // Unexpected key not found. Page must have been moved or modified.
                        // Give up here.
                        continue;
                    }
                    const links = page.linkshere.map((v) => new mw.Title(v.title));
                    this.templateAliasCache[cacheKey].push(...links);
                }
                // templateAliasKeymap setup
                this.templateAliasKeymap = {};
                for (const noticeType in this.templateAliasCache) {
                    for (const title of this.templateAliasCache[noticeType]) {
                        this.templateAliasKeymap[title.getPrefixedDb()] = noticeType;
                    }
                }
                // templateAliasRegExp setup
                const summarizedTitles = [];
                for (const titles of getObjectValues(this.templateAliasCache)) {
                    for (const title of titles) {
                        summarizedTitles.push(title.getPrefixedDb());
                    }
                }
                this.templateAliasRegExp = new RegExp(summarizedTitles.map((v) => `(${mw.util.escapeRegExp(v)})`).join('|'), 'g');
            });
        }
        /**
         * Get the notice type of a given template from its href string, or `undefined` if it
         * is not a valid notice.
         *
         * @param href The href of the template.
         * @return A notice type string.
         */
        static getTemplateNoticeType(href) {
            return this.templateAliasKeymap[href.replace(/^\.\//, '')];
        }
    }
    /**
     * An object mapping notice types to their respective class.
     */
    WikiAttributionNotices.attributionNoticeClasses = {
        copied: CopiedTemplate,
        splitArticle: SplitArticleTemplate,
        mergedFrom: MergedFromTemplate,
        mergedTo: MergedToTemplate,
        backwardsCopy: BackwardsCopyTemplate,
        translatedPage: TranslatedPageTemplate
    };

    /**
     * Renders a MenuLayout responsible for displaying analysis options or tools.
     */
    class AttributionNoticeAddMenu {
        /**
         * @param document
         * @param baseWidget
         */
        constructor(document, baseWidget) {
            this.document = document;
            this.baseWidget = baseWidget;
        }
        /**
         * @inheritDoc
         */
        render() {
            const menuItems = new Map();
            const menuSelectWidget = new OO.ui.MenuSelectWidget({
                hideWhenOutOfView: false,
                verticalPosition: 'below',
                horizontalPosition: 'start',
                widget: this.baseWidget,
                $floatableContainer: this.baseWidget.$element,
                items: Object.keys(attributionNoticeTemplatePages).map((key) => {
                    const item = new OO.ui.MenuOptionWidget({
                        data: key,
                        icon: 'add',
                        // Will automatically use template name as
                        // provided by WikiAttributionNotices.
                        label: `{{${attributionNoticeTemplatePages[key]}}}`,
                        flags: ['progressive']
                    });
                    menuItems.set(item, key);
                    return item;
                })
            });
            menuSelectWidget.on('select', () => {
                // Not a multiselect menu; cast the result to OptionWidget.
                const selected = menuSelectWidget.findSelectedItem();
                if (selected) {
                    const type = selected.getData();
                    const spot = this.document.findNoticeSpot(type);
                    this.document.insertNewNotice(type, spot);
                    // Clear selections.
                    menuSelectWidget.selectItem();
                }
            });
            // Disables clipping (allows the menu to be wider than the button)
            menuSelectWidget.toggleClipping(false);
            this.baseWidget.on('click', () => {
                menuSelectWidget.toggle(true);
            });
            return unwrapWidget(menuSelectWidget);
        }
    }

    let InternalAttributionNoticesEmptyPage;
    /**
     * Initializes the process element.
     */
    function initAttributionNoticesEmptyPage() {
        InternalAttributionNoticesEmptyPage = class AttributionNoticesEmptyPage extends OO.ui.PageLayout {
            /**
             * @param config Configuration to be passed to the element.
             */
            constructor(config) {
                super('cte-no-templates', {});
                this.parent = config.parent;
                this.parsoid = config.parsoid;
                const addListener = this.parent.layout.on('add', () => {
                    for (const name of Object.keys(this.parent.layout.pages)) {
                        if (name !== 'cte-no-templates' && this.outlineItem !== null) {
                            // Pop this page out if a page exists.
                            this.parent.layout.removePages([this]);
                            this.parent.layout.off(addListener);
                            return;
                        }
                    }
                });
                // Render the page.
                const add = new OO.ui.ButtonWidget({
                    icon: 'add',
                    label: mw.msg('deputy.ante.empty.add'),
                    flags: ['progressive']
                });
                this.parent.$overlay.append(new AttributionNoticeAddMenu(this.parsoid, add).render());
                this.$element.append(h_1("h3", null, mw.msg('deputy.ante.empty.header')), h_1("p", null, mw.message(this.parsoid.originalCount > 0 ?
                    'deputy.ante.empty.removed' :
                    'deputy.ante.empty.none').text()), add.$element);
            }
            /**
             * Sets up the outline item of this page. Used in the BookletLayout.
             */
            setupOutlineItem() {
                if (this.outlineItem !== undefined) {
                    this.outlineItem.toggle(false);
                }
            }
        };
    }
    /**
     * Creates a new AttributionNoticesEmptyPage.
     *
     * @param config Configuration to be passed to the element.
     * @return A AttributionNoticesEmptyPage object
     */
    function CopiedTemplatesEmptyPage (config) {
        if (!InternalAttributionNoticesEmptyPage) {
            initAttributionNoticesEmptyPage();
        }
        return new InternalAttributionNoticesEmptyPage(config);
    }

    var ParsoidDocument_module = {};

    Object.defineProperty(ParsoidDocument_module, "__esModule", { value: true });
    // ParsoidDocument:start
    /**
     * The root of this wiki's RestBase endpoint. This MUST NOT end with a slash.
     */
    const restBaseRoot = window.restBaseRoot || '/api/rest_';
    /**
     * Encodes text for an API parameter. This performs both an encodeURIComponent
     * and a string replace to change spaces into underscores.
     * @param {string} text
     * @returns {string}
     */
    function encodeAPIComponent(text) {
        return encodeURIComponent(text.replace(/ /g, '_'));
    }
    /**
     * Clones a regular expression.
     * @param regex The regular expression to clone.
     * @returns A new regular expression object.
     */
    function cloneRegex(regex) {
        return new RegExp(regex.source, regex.flags);
    }
    /**
     * A class denoting a transclusion template node (a transcluded template, barring any included
     * text or inline parameters) inside an element with [typeof="mw:Transclusion"].
     */
    class ParsoidTransclusionTemplateNode {
        /**
         * Creates a new ParsoidTransclusionTemplateNode. Can be used later on to add a template
         * into wikitext. To have this node show up in wikitext, append the node's element (using
         * {@link ParsoidTransclusionTemplateNode.element}) to the document of a ParsoidDocument.
         * @param document The document used to generate this node.
         * @param template The template to create. If you wish to generate wikitext as a block-type
         *   transclusion (as long as a format is not provided through TemplateData), append a "\n"
         *   to the end of the template name.
         * @param parameters The parameters to the template.
         * @param autosave
         * @returns A new ParsoidTransclusionTemplateNode.
         */
        static fromNew(document, template, parameters, autosave) {
            const el = document.getDocument().createElement('span');
            const target = { wt: template };
            if (mw === null || mw === void 0 ? void 0 : mw.Title) {
                // If `mediawiki.Title` is loaded, use it.
                target.href = './' + new mw.Title(target.wt, mw.config.get('wgNamespaceIds').template).getPrefixedDb();
            }
            const data = {
                target,
                params: {},
                i: 0
            };
            for (const param in (parameters !== null && parameters !== void 0 ? parameters : {})) {
                const value = parameters[param];
                data.params[param] = {
                    wt: typeof value === 'string' ? value : value.toString()
                };
            }
            el.setAttribute('typeof', 'mw:Transclusion');
            el.setAttribute('data-mw', JSON.stringify({
                parts: [{
                        template: data
                    }]
            }));
            return new ParsoidTransclusionTemplateNode(document, el, data, data.i, autosave);
        }
        /**
         * Create a new ParsoidTransclusionTemplateNode.
         * @param {ParsoidDocument} parsoidDocument
         *     The document handling this transclusion node.
         * @param {HTMLElement} originalElement
         *     The original element where the `data-mw` of this node is found.
         * @param {*} data
         *     The `data-mw` `part.template` of this node.
         * @param {number} i
         *     The `i` property of this node.
         * @param {boolean} autosave
         *     Whether to automatically save parameter and target changes or not.
         */
        constructor(parsoidDocument, originalElement, data, i, autosave = true) {
            this.parsoidDocument = parsoidDocument;
            this.element = originalElement;
            this.data = data;
            this.i = i;
            this.autosave = autosave;
        }
        /**
         * Gets the target of this node.
         * @returns {object} The target of this node, in wikitext and href (for links).
         */
        getTarget() {
            return this.data.target;
        }
        /**
         * Sets the target of this template (in wikitext).
         * @param {string} wikitext
         *   The target template (in wikitext, e.g. `Test/{{FULLPAGENAME}}`).
         */
        setTarget(wikitext) {
            this.data.target.wt = wikitext;
            if (mw === null || mw === void 0 ? void 0 : mw.Title) {
                // If `mediawiki.Title` is loaded, use it.
                this.data.target.href = './' + new mw.Title(wikitext, mw.config.get('wgNamespaceIds').template).getPrefixedDb();
            }
            else {
                // Likely inaccurate. Just remove it to make sent data cleaner.
                delete this.data.target.href;
            }
            if (this.autosave) {
                this.save();
            }
        }
        /**
         * Gets the parameters of this node.
         * @returns {{[key:string]:{wt:string}}} The parameters of this node, in wikitext.
         */
        getParameters() {
            return this.data.params;
        }
        /**
         * Checks if a template has a parameter.
         * @param {string} key The key of the parameter to check.
         * @returns {boolean} `true` if the template has the given parameter
         */
        hasParameter(key) {
            return this.data.params[key] != null;
        }
        /**
         * Gets the value of a parameter.
         * @param {string} key The key of the parameter to check.
         * @returns {string} The parameter value.
         */
        getParameter(key) {
            var _a;
            return (_a = this.data.params[key]) === null || _a === void 0 ? void 0 : _a.wt;
        }
        /**
         * Sets the value for a specific parameter. If `value` is null or undefined,
         * the parameter is removed.
         * @param {string} key The parameter key to set.
         * @param {string} value The new value of the parameter.
         */
        setParameter(key, value) {
            if (value != null) {
                this.data.params[key] = { wt: value };
                if (this.autosave) {
                    this.save();
                }
            }
            else {
                this.removeParameter(key);
            }
        }
        /**
         * Removes a parameter from the template.
         * @param key The parameter key to remove.
         */
        removeParameter(key) {
            if (this.data.params[key] != null) {
                delete this.data.params[key];
            }
            if (this.autosave) {
                this.save();
            }
        }
        /**
         * Fix improperly-set parameters.
         */
        cleanup() {
            for (const key of Object.keys(this.data.params)) {
                const param = this.data.params[key];
                if (typeof param === 'string') {
                    this.data.params[key] = {
                        wt: param
                    };
                }
            }
        }
        /**
         * Removes this node from its element. This will prevent the node from being saved
         * again.
         * @param eraseLine For block templates. Setting this to `true` will also erase a newline
         * that immediately succeeds this template, if one exists. This is useful in ensuring that
         * there are no excesses of newlines in the document.
         */
        destroy(eraseLine) {
            var _a;
            const existingData = JSON.parse(this.element.getAttribute('data-mw'));
            if (existingData.parts.length === 1) {
                const nodeElements = this.parsoidDocument.getNodeElements(this);
                const succeedingTextNode = (_a = nodeElements[nodeElements.length - 1]) === null || _a === void 0 ? void 0 : _a.nextSibling;
                // The element contains nothing else except this node. Destroy the element entirely.
                this.parsoidDocument.destroyParsoidNode(this.element);
                if (eraseLine && succeedingTextNode &&
                    succeedingTextNode.nodeType === Node.TEXT_NODE) {
                    // Erase a starting newline, if one exists
                    succeedingTextNode.nodeValue = succeedingTextNode.nodeValue
                        .replace(/^\n/, '');
                }
            }
            else {
                const partToRemove = existingData.parts.find((part) => { var _a; return ((_a = part.template) === null || _a === void 0 ? void 0 : _a.i) === this.i; });
                if (eraseLine) {
                    const iFront = existingData.parts.indexOf(partToRemove) - 1;
                    const iBack = existingData.parts.indexOf(partToRemove) + 1;
                    let removed = false;
                    if (iBack < existingData.parts.length &&
                        typeof existingData.parts[iBack] === 'string') {
                        // Attempt to remove whitespace from the string in front of the template.
                        if (/^\r?\n/.test(existingData.parts[iBack])) {
                            // Whitespace found, remove it.
                            existingData.parts[iBack] =
                                existingData.parts[iBack].replace(/^\r?\n/, '');
                            removed = true;
                        }
                    }
                    if (!removed && iFront > -1 && typeof existingData.parts[iFront] === 'string') {
                        // Attempt to remove whitespace from the string behind the template.
                        if (/\r?\n$/.test(existingData.parts[iFront])) {
                            // Whitespace found, remove it.
                            existingData.parts[iFront] =
                                existingData.parts[iFront].replace(/\r?\n$/, '');
                        }
                    }
                }
                existingData.parts.splice(existingData.parts.indexOf(partToRemove), 1);
                this.element.setAttribute('data-mw', JSON.stringify(existingData));
            }
        }
        /**
         * Saves this node (including modifications) back into its element.
         */
        save() {
            this.cleanup();
            const existingData = JSON.parse(this.element.getAttribute('data-mw'));
            existingData.parts.find((part) => { var _a; return ((_a = part.template) === null || _a === void 0 ? void 0 : _a.i) === this.i; }).template = this.data;
            this.element.setAttribute('data-mw', JSON.stringify(existingData));
        }
    }
    /**
     * A class containing an {@link HTMLIFrameElement} along with helper functions
     * to make manipulation easier.
     */
    class ParsoidDocument extends EventTarget {
        /**
         * Create a new ParsoidDocument instance from a page on-wiki.
         * @param {string} page The page to load.
         * @param {object} options Options for frame loading.
         * @param {boolean} options.reload
         *   Whether the current page should be discarded and reloaded.
         * @param options.allowMissing
         *   Set to `false` to avoid loading a blank document if the page does not exist.
         */
        static async fromPage(page, options = {}) {
            const doc = new ParsoidDocument();
            await doc.loadPage(page, options);
            return doc;
        }
        /**
         * Create a new ParsoidDocument instance from plain HTML.
         * @param {string} page The name of the page.
         * @param {string} html The HTML to use.
         * @param restBaseUri The relative URI to the RESTBase instance to be used for transforms.
         * @param {boolean} wrap Set to `false` to avoid wrapping the HTML within the body.
         */
        static async fromHTML(page, html, restBaseUri, wrap = true) {
            const doc = new ParsoidDocument();
            await doc.loadHTML(page, wrap ? ParsoidDocument.blankDocument : html, restBaseUri);
            if (wrap) {
                doc.document.getElementsByTagName('body')[0].innerHTML = html;
            }
            return doc;
        }
        /**
         * Creates a new ParsoidDocument from a blank page.
         * @param {string} page The name of the page.
         * @param restBaseUri
         */
        static async fromBlank(page, restBaseUri) {
            const doc = new ParsoidDocument();
            await doc.loadHTML(page, ParsoidDocument.blankDocument, restBaseUri);
            return doc;
        }
        /**
         * Creates a new ParsoidDocument from wikitext.
         * @param {string} page The page of the document.
         * @param {string} wikitext The wikitext to load.
         * @param restBaseUri
         */
        static async fromWikitext(page, wikitext, restBaseUri) {
            const doc = new ParsoidDocument();
            await doc.loadWikitext(page, wikitext, restBaseUri);
            return doc;
        }
        /**
         * Get additional request options to be patched onto RESTBase API calls.
         * Extend this class to modify this.
         * @protected
         */
        getRequestOptions() {
            return {
                headers: {
                    'Api-User-Agent': 'parsoid-document/2.0.0 (https://github.com/ChlodAlejandro/parsoid-document; chlod@chlod.net)'
                }
            };
        }
        /**
         * @returns `true` if the page is a redirect. `false` if otherwise.
         */
        get redirect() {
            return this.document &&
                this.document.querySelector("[rel='mw:PageProp/redirect']") !== null;
        }
        /**
         * Create a new ParsoidDocument instance.
         */
        constructor() {
            super();
            this.iframe = document.createElement('iframe');
            Object.assign(this.iframe.style, {
                width: '0',
                height: '0',
                border: '0',
                position: 'fixed',
                top: '0',
                left: '0'
            });
            this.iframe.addEventListener('load', () => {
                if (this.iframe.contentWindow.document.URL === 'about:blank') {
                    // Blank document loaded. Ignore.
                    return;
                }
                /**
                 * The document of this ParsoidDocument's IFrame.
                 * @type {Document}
                 * @protected
                 */
                this.document = this.iframe.contentWindow.document;
                this.$document = $(this.document);
                this.setupJquery(this.$document);
                this.buildIndex();
                if (this.observer) {
                    // This very much assumes that the MutationObserver is still connected.
                    // Yes, this is quite an assumption, but should not be a problem during normal use.
                    // If only MutationObserver had a `.connected` field...
                    this.observer.disconnect();
                }
                this.observer = new MutationObserver(() => {
                    this.buildIndex();
                });
                this.observer.observe(this.document.getElementsByTagName('body')[0], {
                    // Listen for ALL DOM mutations.
                    attributes: true,
                    childList: true,
                    subtree: true
                });
                // Replace the page title. Handles redirects.
                if (this.document.title) {
                    this.page = (mw === null || mw === void 0 ? void 0 : mw.Title) ?
                        new mw.Title(this.document.title).getPrefixedText() :
                        this.document.title;
                }
            });
            document.getElementsByTagName('body')[0].appendChild(this.iframe);
        }
        /**
         * Set up a JQuery object for this window.
         * @param $doc The JQuery object to set up.
         * @returns The JQuery object.
         */
        setupJquery($doc) {
            // noinspection JSPotentiallyInvalidConstructorUsage
            const $proto = $doc.constructor.prototype;
            /* eslint-disable-next-line @typescript-eslint/no-this-alias */
            const doc = this;
            $proto.parsoidNode = function () {
                if (this.length === 1) {
                    return doc.findParsoidNode(this[0]);
                }
                else {
                    return this.map((node) => doc.findParsoidNode(node));
                }
            };
            $proto.parsoid = function () {
                /**
                 * Processes an element and extracts its transclusion parts.
                 * @param {HTMLElement} element Element to process.
                 * @returns The transclusion parts.
                 */
                function process(element) {
                    const rootNode = doc.findParsoidNode(element);
                    const mwData = JSON.parse(rootNode.getAttribute('data-mw'));
                    return mwData.parts.map((part) => {
                        if (part.template) {
                            return new ParsoidTransclusionTemplateNode(this, rootNode, part.template, part.template.i);
                        }
                        else {
                            return part;
                        }
                    });
                }
                if (this.length === 1) {
                    return process(this[0]);
                }
                else {
                    return this.map((element) => process(element));
                }
            };
            return $doc;
        }
        /**
         * Notify the user of a document loading error.
         * @param {Error} error An error object.
         */
        notifyLoadError(error) {
            if (mw === null || mw === void 0 ? void 0 : mw.notify) {
                mw.notify([
                    (() => {
                        const a = document.createElement('span');
                        a.innerText = 'An error occurred while loading a Parsoid document: ';
                        return a;
                    })(),
                    (() => {
                        const b = document.createElement('b');
                        b.innerText = error.message;
                        return b;
                    })()
                ], {
                    tag: 'parsoidDocument-error',
                    type: 'error'
                });
            }
            throw error;
        }
        /**
         * Loads a wiki page with this ParsoidDocument.
         * @param {string} page The page to load.
         * @param {object} options Options for frame loading.
         * @param {boolean} options.reload
         *   Whether the current page should be discarded and reloaded.
         * @param options.allowMissing
         *   Set to `false` to avoid loading a blank document if the page does not exist.
         * @param options.restBaseUri
         *   A relative or absolute URI to the wiki's RESTBase root. This is
         *   `/api/rest_` by default, though the `window.restBaseRoot` variable
         *   can modify it.
         * @param options.requestOptions
         *   Options to pass to the `fetch` request.
         * @param options.followRedirects
         *   Whether to follow page redirects or not.
         */
        async loadPage(page, options = {}) {
            var _a, _b, _c;
            if (this.document && options.reload !== true) {
                throw new Error('Attempted to reload an existing frame.');
            }
            this.restBaseUri = (_a = options.restBaseUri) !== null && _a !== void 0 ? _a : restBaseRoot;
            return fetch(`${this.restBaseUri}v1/page/html/${encodeAPIComponent(page)}?stash=true&redirect=${options.followRedirects !== false ? 'true' : 'false'}&t=${Date.now()}`, Object.assign({
                cache: 'no-cache'
            }, (_b = this.getRequestOptions()) !== null && _b !== void 0 ? _b : {}, (_c = options.requestOptions) !== null && _c !== void 0 ? _c : {}))
                .then((data) => {
                /**
                 * The ETag of this iframe's content.
                 * @type {string}
                 */
                this.etag = data.headers.get('ETag');
                if (data.status === 404 && options.allowMissing !== false) {
                    this.fromExisting = false;
                    // A Blob is used in order to allow cross-frame access without changing
                    // the origin of the frame.
                    return Promise.resolve(ParsoidDocument.defaultDocument);
                }
                else {
                    this.fromExisting = true;
                    return data.text();
                }
            })
                .then((html) => this.loadHTML(page, html, this.restBaseUri))
                .catch(this.notifyLoadError);
        }
        /**
         * Load a document from wikitext.
         * @param {string} page The page title of this document.
         * @param {string} wikitext The wikitext to load.
         * @param restBaseUri
         */
        async loadWikitext(page, wikitext, restBaseUri) {
            var _a;
            this.restBaseUri = restBaseUri !== null && restBaseUri !== void 0 ? restBaseUri : restBaseRoot;
            return fetch(`${this.restBaseUri}v1/transform/wikitext/to/html/${encodeAPIComponent(page)}?t=${Date.now()}`, Object.assign((_a = this.getRequestOptions()) !== null && _a !== void 0 ? _a : {}, {
                cache: 'no-cache',
                method: 'POST',
                body: (() => {
                    const formData = new FormData();
                    formData.set('wikitext', wikitext);
                    formData.set('body_only', 'false');
                    return formData;
                })()
            }))
                .then((data) => {
                /**
                 * The ETag of this iframe's content.
                 * @type {string}
                 */
                this.etag = data.headers.get('ETag');
                this.fromExisting = false;
                return data.text();
            })
                .then((html) => this.loadHTML(page, html, this.restBaseUri))
                .catch(this.notifyLoadError);
        }
        /**
         * Load a document from HTML.
         * @param {string} page The loaded page's name.
         * @param {string} html The page's HTML.
         * @param restBaseUri A relative or absolute URI to the wiki's RESTBase root.
         */
        async loadHTML(page, html, restBaseUri) {
            this.restBaseUri = restBaseUri !== null && restBaseUri !== void 0 ? restBaseUri : restBaseRoot;
            // A Blob is used in order to allow cross-frame access without changing
            // the origin of the frame.
            this.iframe.src = URL.createObjectURL(new Blob([html], { type: 'text/html' }));
            this.page = page;
            return new Promise((res) => {
                this.iframe.addEventListener('load', () => {
                    res();
                }, { once: true });
            });
        }
        /**
         * Destroys the frame and pops it off of the DOM (if inserted).
         * Silently fails if the frame has not yet been built.
         */
        destroy() {
            if (this.iframe && this.iframe.parentElement) {
                this.iframe.parentElement.removeChild(this.iframe);
                this.iframe = undefined;
            }
        }
        /**
         * Reloads the page. This will destroy any modifications made to the document.
         */
        async reload() {
            const page = this.page;
            this.page = undefined;
            return this.loadPage(page, { reload: true });
        }
        /**
         * Clears the frame for a future reload. This will later permit `loadPage` and
         * other related functions to run without the `reload` option.
         */
        reset() {
            // Reset the page
            this.page = undefined;
            // Reset the element index
            this.elementIndex = undefined;
            // Reset DOM-related fields
            this.document = undefined;
            this.$document = undefined;
            this.etag = undefined;
            this.fromExisting = undefined;
            // Disconnect the mutation observer
            this.observer.disconnect();
            this.observer = undefined;
            // Reset the IFrame
            this.iframe.src = 'about:blank';
            // By this point, this whole thing should be a clean state.
        }
        /**
         * Constructs the {@link ParsoidDocument#elementIndex} from the current document.
         */
        buildIndex() {
            if (this.document == null) {
                throw new Error("Can't perform operations without a loaded page.");
            }
            this.elementIndex = {};
            const nodes = this.document.querySelectorAll('[typeof^=\'mw:\']');
            nodes.forEach((node) => {
                node.getAttribute('typeof')
                    .split(/\s+/g)
                    .map((type) => type.replace(/^mw:/, ''))
                    .forEach((type) => {
                    if (this.elementIndex[type] == null) {
                        this.elementIndex[type] = [];
                    }
                    this.elementIndex[type].push(node);
                });
            });
        }
        /**
         * Gets the `<section>` HTMLElement given a section ID.
         * @param id The ID of the section
         * @returns The HTMLElement of the section. If the section cannot be found, `null`.
         */
        getSection(id) {
            return this.document.querySelector(`section[data-mw-section-id="${id}"]`);
        }
        /**
         * Finds a template in the loaded document.
         * @param {string|RegExp} templateName The name of the template to look for.
         * @param {boolean} hrefMode Use the href instead of the wikitext to search for templates.
         * @returns {HTMLElement} A list of elements.
         */
        findTemplate(templateName, hrefMode = false) {
            var _a;
            if (this.document == null) {
                throw new Error("Can't perform operations without a loaded page.");
            }
            const templates = (_a = this.elementIndex) === null || _a === void 0 ? void 0 : _a.Transclusion;
            if (templates == null || templates.length === 0) {
                return [];
            }
            return templates.map((node) => {
                const mwData = JSON.parse(node.getAttribute('data-mw'));
                const matching = mwData.parts.filter((part) => {
                    var _a;
                    if (part.template == null) {
                        return false;
                    }
                    if (((_a = part.template.target) === null || _a === void 0 ? void 0 : _a.href) == null) {
                        // Parser function or magic word, not a template transclusion
                        return false;
                    }
                    const compareTarget = part.template.target[hrefMode ? 'href' : 'wt'];
                    if (typeof templateName !== 'string') {
                        return cloneRegex(templateName).test(compareTarget.trim());
                    }
                    else {
                        return templateName === compareTarget.trim();
                    }
                });
                if (matching.length > 0) {
                    return matching.map((part) => {
                        return new ParsoidTransclusionTemplateNode(this, node, part.template, part.template.i);
                    });
                }
                else {
                    return [];
                }
            }).reduce((a, b) => a.concat(b), []);
        }
        /**
         * Finds the element with the "data-mw" attribute containing the element
         * passed into the function.
         * @param {HTMLElement} element
         *   The element to find the parent of. This must be a member of the
         *   ParsoidDocument's document.
         * @returns {HTMLElement} The element responsible for showing the given element.
         */
        findParsoidNode(element) {
            let pivot = element;
            while (pivot.getAttribute('about') == null) {
                if (pivot.parentElement == null) {
                    // Dead end.
                    throw new Error('Reached root of DOM while looking for original Parsoid node.');
                }
                pivot = pivot.parentElement;
            }
            return this.document.querySelector(`[about="${pivot.getAttribute('about')}"][data-mw]`);
        }
        /**
         * Get HTML elements that are associated to a specific Parsoid node using its
         * `about` attribute.
         * @param node The node to get the elements of
         * @returns All elements that match the `about` of the given node.
         */
        getNodeElements(node) {
            return Array.from(this.document.querySelectorAll(`[about="${(node instanceof ParsoidTransclusionTemplateNode ? node.element : node)
            .getAttribute('about')}"]`));
        }
        /**
         * Deletes all elements that have the same `about` attribute as the given element.
         * This effectively deletes an element, be it a transclusion set, file, section,
         * or otherwise.
         * @param element
         */
        destroyParsoidNode(element) {
            if (element.hasAttribute('about')) {
                this.getNodeElements(element).forEach((nodeElement) => {
                    nodeElement.parentElement.removeChild(nodeElement);
                });
            }
            else {
                // No "about" attribute. Just remove that element only.
                element.parentElement.removeChild(element);
            }
        }
        /**
         * Converts the contents of this document to wikitext.
         * @returns {Promise<string>} The wikitext of this document.
         */
        async toWikitext() {
            var _a;
            // this.restBaseUri should be set.
            let target = `${this.restBaseUri}v1/transform/html/to/wikitext/${encodeAPIComponent(this.page)}`;
            if (this.fromExisting) {
                target += `/${+(/(\d+)$/.exec(this.document.documentElement.getAttribute('about'))[1])}`;
            }
            const requestOptions = this.getRequestOptions();
            return fetch(target, Object.assign(requestOptions, {
                method: 'POST',
                headers: Object.assign((_a = requestOptions.headers) !== null && _a !== void 0 ? _a : {}, { 'If-Match': this.fromExisting ? this.etag : undefined }),
                body: (() => {
                    const data = new FormData();
                    data.set('html', this.document.documentElement.outerHTML);
                    data.set('scrub_wikitext', 'true');
                    data.set('stash', 'true');
                    return data;
                })()
            })).then((data) => data.text());
        }
        /**
         * Get the {@link Document} object of this ParsoidDocument.
         * @returns {Document} {@link ParsoidDocument#document}
         */
        getDocument() {
            return this.document;
        }
        /**
         * Get the JQuery object associated with this ParsoidDocument.
         * @returns {*} {@link ParsoidDocument#$document}
         */
        getJQuery() {
            return this.$document;
        }
        /**
         * Get the IFrame element of this ParsoidDocument.
         * @returns {HTMLIFrameElement} {@link ParsoidDocument#iframe}
         */
        getIframe() {
            return this.iframe;
        }
        /**
         * Get the page name of the currently-loaded page.
         * @returns {string} {@link ParsoidDocument#page}
         */
        getPage() {
            return this.page;
        }
        /**
         * Get the element index of this ParsoidDocument.
         * @returns {{ [p: string]: HTMLElement[] }} {@link ParsoidDocument#elementIndex}
         */
        getElementIndex() {
            return this.elementIndex;
        }
        /**
         * Check if this element exists on-wiki or not.
         * @returns {boolean} {@link ParsoidDocument#fromExisting}
         */
        isFromExisting() {
            return this.fromExisting;
        }
    }
    ParsoidDocument.Node = ParsoidTransclusionTemplateNode;
    /**
     * A blank Parsoid document, with a section 0.
     * @type {string}
     */
    ParsoidDocument.blankDocument = '<html><body><section data-mw-section-id="0"></section></body></html>';
    /**
     * The default document to create if a page was not found.
     * @type {string}
     */
    ParsoidDocument.defaultDocument = ParsoidDocument.blankDocument;
    // ParsoidDocument:end
    var _default = ParsoidDocument_module.default = ParsoidDocument;

    /**
     * Returns the last item of an array.
     *
     * @param array The array to get the last element from
     * @return The last element of the array
     */
    function last(array) {
        return array[array.length - 1];
    }

    /**
     * An event dispatched when a template inside a `CopiedTemplateEditorDialog` is inserted.
     */
    class TemplateInsertEvent extends Event {
        /**
         * @param template The template that was inserted
         * @param eventInitDict
         */
        constructor(template, eventInitDict) {
            super('templateInsert', eventInitDict);
            this.template = template;
        }
    }

    /**
     * Extension class of ParsoidDocument's node. Used to type `parsoidDocument` in the
     * below function. Since the original node is always instantiated with `this`, it
     * can be assumed that `parsoidDocument` is a valid CTEParsoidDocument.
     */
    class CTEParsoidTransclusionTemplateNode extends _default.Node {
        /**
         * @inheritDoc
         */
        static fromNew(document, template, parameters, autosave) {
            return this.upgradeNode(super.fromNew(document, template, parameters, autosave), document);
        }
        /**
         * Upgrades a vanilla ParsoidDocument.Node to a CTEParsoidTransclusionTemplateNode.
         *
         * @param node The node to upgrade
         * @param document The document to attach
         * @return A CTEParsoidTransclusionTemplateNode
         */
        static upgradeNode(node, document) {
            return new CTEParsoidTransclusionTemplateNode(document, node.element, node.data, node.i, node.autosave);
        }
    }

    /**
     * Creates blank attribution notices. Its own class to avoid circular dependencies.
     */
    class TemplateFactory {
        /**
         * Get the template wikitext (`target.wt`) of a given notice type.
         *
         * @param type
         * @return The wikitext of the template's target page
         */
        static getTemplateWikitext(type) {
            return WikiAttributionNotices.attributionNoticeTemplates[type].getNamespaceId() ===
                nsId('template') ?
                // If in the "Template" namespace, "Copied"
                WikiAttributionNotices.attributionNoticeTemplates[type].getMainText() :
                // If not in the "Template" namespace, "Namespace:Copied"
                WikiAttributionNotices.attributionNoticeTemplates[type].getPrefixedText();
        }
        /**
         * Creates a new {@link CopiedTemplate}
         *
         * @param document
         * @return A new CopiedTemplate
         */
        static copied(document) {
            const templateWikitext = TemplateFactory.getTemplateWikitext('copied');
            const node = CTEParsoidTransclusionTemplateNode.fromNew(document, templateWikitext, {
                // Pre-fill with target page
                to: new mw.Title(document.getPage()).getSubjectPage().getPrefixedText()
            });
            node.element.setAttribute('about', `N${TemplateFactory.noticeCount++}`);
            node.element.classList.add('copiednotice');
            return new CopiedTemplate(node);
        }
        /**
         * Creates a new {@link SplitArticleTemplate}
         *
         * @param document
         * @return A new SplitArticleTemplate
         */
        static splitArticle(document) {
            const templateWikitext = TemplateFactory.getTemplateWikitext('splitArticle');
            const node = CTEParsoidTransclusionTemplateNode.fromNew(document, templateWikitext, {
                from: new mw.Title(document.getPage()).getSubjectPage().getPrefixedText(),
                // Blank string to trigger row creation
                to: ''
            });
            node.element.setAttribute('about', `N${TemplateFactory.noticeCount++}`);
            node.element.classList.add('box-split-article');
            return new SplitArticleTemplate(node);
        }
        /**
         * Creates a new {@link MergedFromTemplate}
         *
         * @param document
         * @return A new MergedFromTemplate
         */
        static mergedFrom(document) {
            const templateWikitext = TemplateFactory.getTemplateWikitext('mergedFrom');
            const node = CTEParsoidTransclusionTemplateNode.fromNew(document, templateWikitext, {});
            node.element.setAttribute('about', `N${TemplateFactory.noticeCount++}`);
            node.element.classList.add('box-merged-from');
            return new MergedFromTemplate(node);
        }
        /**
         * Creates a new {@link MergedToTemplate}
         *
         * @param document
         * @return A new MergedToTemplate
         */
        static mergedTo(document) {
            const templateWikitext = TemplateFactory.getTemplateWikitext('mergedTo');
            const node = CTEParsoidTransclusionTemplateNode.fromNew(document, templateWikitext, {});
            node.element.setAttribute('about', `N${TemplateFactory.noticeCount++}`);
            node.element.classList.add('box-merged-to');
            return new MergedToTemplate(node);
        }
        /**
         * Creates a new {@link BackwardsCopyTemplate}
         *
         * @param document
         * @return A new MergedToTemplate
         */
        static backwardsCopy(document) {
            const templateWikitext = TemplateFactory.getTemplateWikitext('backwardsCopy');
            const node = CTEParsoidTransclusionTemplateNode.fromNew(document, templateWikitext, {
                // Blank string to trigger row creation
                title: ''
            });
            node.element.setAttribute('about', `N${TemplateFactory.noticeCount++}`);
            node.element.classList.add('box-merged-to');
            return new BackwardsCopyTemplate(node);
        }
        /**
         * Creates a new {@link TranslatedPageTemplate}
         *
         * @param document
         * @return A new MergedToTemplate
         */
        static translatedPage(document) {
            const templateWikitext = TemplateFactory.getTemplateWikitext('translatedPage');
            const node = CTEParsoidTransclusionTemplateNode.fromNew(document, templateWikitext);
            node.element.setAttribute('about', `N${TemplateFactory.noticeCount++}`);
            node.element.classList.add('box-translated-page');
            return new TranslatedPageTemplate(node);
        }
    }
    /**
     * Simply increments when notices are added. This gives specific notices a
     * human-friendly identifier.
     */
    TemplateFactory.noticeCount = 1;

    /**
     * Moves a value as determined by an index of the array to the start of the array.
     * Mutates the original array.
     *
     * @param array The array to use
     * @param index The index of the value to move to the start of the array
     * @return The reordered array.
     */
    function moveToStart(array, index) {
        const el = array[index];
        array.splice(index, 1);
        array.splice(0, 0, el);
        return array;
    }

    function organize(objects, keyer) {
        const finalObj = {};
        for (const obj of objects) {
            const key = keyer(obj);
            if (!finalObj[key]) {
                finalObj[key] = [];
            }
            finalObj[key].push(obj);
        }
        return finalObj;
    }

    /**
     * An object containing an {@link HTMLIFrameElement} along with helper functions
     * to make manipulation easier.
     */
    class CTEParsoidDocument extends _default {
        /**
         * Creates a new CTE-specific ParsoidDocument. This extends from the existing
         * ParsoidDocument with functions specifically catered for pages that have
         * {{copied}} (or will have) templates.
         */
        constructor() {
            super();
            /**
             * A map of all Parsoid HTML elements and their attribution notices. When notices are
             * detected, they are added here. ParsoidTemplateTransclusionNode is not used here
             * since they are regenerated every time `findTemplate` is called.
             */
            this.notices = new Map();
            /**
             * The original number of {{copied}} notices in the document.
             */
            this.originalCount = null;
            // Event listeners should be fired synchronously. Load listener found in
            // `super` should have been run by this point.
            this.iframe.addEventListener('load', () => {
                if (this.iframe.contentWindow.document.URL === 'about:blank') {
                    // Blank document loaded. Ignore.
                    return;
                }
                const notices = this.findNoticeType('copied');
                this.originalCount = notices.length;
                if (this.redirect) {
                    // Move the redirect line out of the way to avoid breaking redirects.
                    const p = document.createElement('p');
                    const redirect = this.iframe.contentWindow.document
                        .querySelector('[rel="mw:PageProp/Redirect"]');
                    redirect.insertAdjacentElement('afterend', p);
                    p.appendChild(redirect);
                }
            });
        }
        /**
         * @inheritDoc
         * @protected
         */
        getRequestOptions() {
            var _a, _b;
            const ro = super.getRequestOptions();
            return {
                headers: {
                    'Api-User-Agent': `${MwApi.USER_AGENT} ${(_b = (_a = ro.headers) === null || _a === void 0 ? void 0 : _a['Api-User-Agent']) !== null && _b !== void 0 ? _b : ''}`
                }
            };
        }
        /**
         * @inheritDoc
         */
        reset() {
            super.reset();
            this.originalCount = undefined;
            this.notices.clear();
        }
        /**
         * Finds all content attribution notices in the talk page. This includes {{copied}},
         * {{merged to}}, {{merged from}}, etc.
         *
         * @return An array of AttributionNotice objects.
         */
        findNotices() {
            this.buildIndex();
            // Used instead of `this.notices.values()` to exclude nodes that are no longer on the DOM.
            const notices = [];
            for (const node of this.findTemplate(WikiAttributionNotices.templateAliasRegExp, true)) {
                if (!this.notices.has(node.element)) {
                    // Notice not yet cached, but this is an attribution notice.
                    // Now to determine what type.
                    const type = WikiAttributionNotices.getTemplateNoticeType(node.getTarget().href);
                    const noticeInstance = new (WikiAttributionNotices.attributionNoticeClasses[type])(CTEParsoidTransclusionTemplateNode.upgradeNode(node, this));
                    this.notices.set(node.element, noticeInstance);
                }
                notices.push(this.notices.get(node.element));
            }
            return notices;
        }
        /**
         * Find all notices which have rows using their 'href' fields.
         *
         * @return All found {@link RowedAttributionNotice}s
         */
        findRowedNoticesByHref() {
            return organize(this.findNotices().filter(v => v instanceof RowedAttributionNotice), (v) => v.node.getTarget().href);
        }
        /**
         * Finds this document's {{copied}} notices.
         *
         * @param type
         * @return An array of all CopiedTemplate objects found
         */
        findNoticeType(type) {
            return this.findNotices().filter((notice) => notice instanceof
                WikiAttributionNotices.attributionNoticeClasses[type]);
        }
        /**
         * Look for a good spot to place a {{copied}} template.
         *
         * @param type The type of the notice to look a spot for.
         * @return A spot to place the template, `null` if a spot could not be found.
         */
        findNoticeSpot(type) {
            var _a, _b;
            // TODO: Just use a simple "if" for {{translated page}}.
            const positionIndices = {
                copied: 0,
                splitArticle: 1,
                mergedFrom: 2,
                mergedTo: 3,
                backwardsCopy: 4,
                translatedPage: 5
            };
            const positionIndex = positionIndices[type];
            const variableSpots = [
                [
                    positionIndex >= positionIndices.copied ? 'afterend' : 'beforebegin',
                    positionIndex >= positionIndices.copied ?
                        last(this.document.querySelectorAll('.copiednotice')) :
                        this.document.querySelector('.copiednotice')
                ],
                [
                    positionIndex >= positionIndices.splitArticle ? 'afterend' : 'beforebegin',
                    positionIndex >= positionIndices.splitArticle ?
                        last(this.document.querySelectorAll('.box-split-article')) :
                        this.document.querySelector('.box-split-article')
                ],
                [
                    positionIndex >= positionIndices.mergedFrom ? 'afterend' : 'beforebegin',
                    positionIndex >= positionIndices.mergedFrom ?
                        last(this.document.querySelectorAll('.box-merged-from')) :
                        this.document.querySelector('.box-merged-from')
                ],
                [
                    positionIndex >= positionIndices.mergedTo ? 'afterend' : 'beforebegin',
                    positionIndex >= positionIndices.mergedTo ?
                        last(this.document.querySelectorAll('.box-merged-to')) :
                        this.document.querySelector('.box-merged-to')
                ],
                [
                    positionIndex >= positionIndices.backwardsCopy ? 'afterend' : 'beforebegin',
                    positionIndex >= positionIndices.backwardsCopy ?
                        last(this.document.querySelectorAll('.box-backwards-copy')) :
                        this.document.querySelector('.box-backwards-copy')
                ],
                [
                    positionIndex >= positionIndices.translatedPage ? 'afterend' : 'beforebegin',
                    positionIndex >= positionIndices.translatedPage ?
                        last(this.document.querySelectorAll('.box-translated-page')) :
                        this.document.querySelector('.box-translated-page')
                ]
            ];
            // Move everything after the template type we're looking for to the start of the array.
            // Also place the exact type we're looking for at the top of the array.
            // This prioritizes the highest (by position) template in the page.
            const afterSpots = variableSpots.splice(positionIndex + 1, variableSpots.length - positionIndex + 1);
            const beforeSpots = variableSpots.splice(0, positionIndex).reverse();
            moveToStart(variableSpots, 0);
            variableSpots.push(...beforeSpots, ...afterSpots);
            const possibleSpots = [
                ...variableSpots,
                // After the {{to do}} template
                ['afterend', last(this.document.querySelectorAll('.t-todo'))],
                // After the WikiProject banner shell
                ['afterend', this.document.querySelector('.wpbs') ? last(this.document.querySelectorAll(`[about="${this.document.querySelector('.wpbs')
                    .getAttribute('about')}"]`)) : null],
                // After all WikiProject banners
                ['afterend', last(this.document.querySelectorAll('.wpb'))],
                // After the last talk page message box that is not a small box
                ['afterend', last(this.document.querySelectorAll('[data-mw-section-id="0"] .tmbox:not(.mbox-small):not(.talkheader)'))],
                // After the talk page header
                ['afterend', this.document.querySelector('.talkheader')],
                // After the rcat shell
                ['afterend', this.document.querySelector('.box-Redirect_category_shell')],
                // After rcats
                ['afterend', last(this.document.querySelectorAll('.rcat'))],
                // After the #REDIRECT line
                ['afterend', (_a = this.document.querySelector('[rel="mw:PageProp/redirect"]')) === null || _a === void 0 ? void 0 : _a.parentElement],
                // At the start of the talk page
                ['afterbegin', this.document.querySelector('section[data-mw-section-id="0"]')]
            ];
            for (const spot of possibleSpots) {
                if (spot[1] != null) {
                    if (spot[1].hasAttribute('data-mw') ||
                        (!spot[1].getAttribute('about') &&
                            !spot[1].getAttribute('id'))) {
                        return spot;
                    }
                    else {
                        const identifier = ((_b = spot[1].getAttribute('about')) !== null && _b !== void 0 ? _b : spot[1].getAttribute('id')).replace(/^#/, '');
                        // Find the last element from that specific transclusion.
                        const transclusionRoot = last(this.document.querySelectorAll(`#${identifier}, [about="#${identifier}"]`));
                        return [
                            spot[0],
                            transclusionRoot
                        ];
                    }
                }
            }
            return null;
        }
        /**
         * Inserts a new attribution notice of a given type.
         *
         * @param type A notice type
         * @param spot The spot to place the template.
         * @param spot."0" See {@link CTEParsoidDocument.findNoticeSpot()}[0]
         * @param spot."1" See {@link CTEParsoidDocument.findNoticeSpot()}[1]
         */
        insertNewNotice(type, [position, element]) {
            const template = {
                copied: TemplateFactory.copied,
                splitArticle: TemplateFactory.splitArticle,
                mergedFrom: TemplateFactory.mergedFrom,
                mergedTo: TemplateFactory.mergedTo,
                backwardsCopy: TemplateFactory.backwardsCopy,
                translatedPage: TemplateFactory.translatedPage
            }[type](this);
            // Insert.
            element.insertAdjacentElement(position, template.element);
            this.notices.set(template.element, template);
            this.dispatchEvent(new TemplateInsertEvent(template));
        }
    }
    CTEParsoidDocument.addedRows = 1;
    /**
     * Extremely minimalist valid Parsoid document. This includes a section 0
     * element for findCopiedNoticeSpot.
     *
     * @type {string}
     */
    CTEParsoidDocument.defaultDocument = '<html><body><section data-mw-section-id="0"></section></body></html>';

    /**
     * Converts a normal error into an OO.ui.Error for ProcessDialogs.
     *
     * @param error A plain error object.
     * @param config Error configuration.
     * @param config.recoverable Whether or not the error is recoverable.
     * @param config.warning Whether or not the error is a warning.
     * @return An OOUI Error.
     */
    function errorToOO(error, config) {
        return new OO.ui.Error(error.message, config);
    }

    const exitBlockList = [];
    /**
     * Used to block an impending exit.
     *
     * @param event The unload event
     * @return `false`.
     */
    const exitBlock = (event) => {
        if (exitBlockList.length > 0) {
            event.preventDefault();
            return event.returnValue = false;
        }
    };
    window.addEventListener('beforeunload', exitBlock);
    /**
     * Blocks navigation to prevent data loss. This function takes in a
     * `key` parameter to identify which parts of the tool are blocking navigation.
     * The exit block will refuse to unlatch from the document if all keys are not
     * released with `unblockExit`.
     *
     * If no key is provided, this will unconditionally set the block. Running
     * any operation that updates the block list (e.g. `unblockExit` with a key
     * not blocked) will immediately unblock the page.
     *
     * @param key The key of the exit block.
     */
    function blockExit(key) {
        {
            if (exitBlockList.indexOf(key) === -1) {
                exitBlockList.push(key);
            }
        }
    }
    /**
     * Unblocks navigation. This function takes in a `key` parameter to identify
     * which part of the tool is no longer requiring a block. If other parts of
     * the tool still require blocking, the unblock function will remain on the
     * document.
     *
     * If no key is provided, this will dump all keys and immediate unblock exit.
     *
     * @param key The key of the exit block.
     */
    function unblockExit(key) {
        {
            const keyIndex = exitBlockList.indexOf(key);
            if (keyIndex !== -1) {
                exitBlockList.splice(keyIndex, 1);
            }
        }
    }

    /**
     * Appends extra information to an edit summary (also known as the "advert").
     *
     * @param editSummary The edit summary
     * @param config The user's configuration. Used to get the "danger mode" setting.
     * @return The decorated edit summary (in wikitext)
     */
    function decorateEditSummary (editSummary, config) {
        var _a;
        const dangerMode = (_a = config === null || config === void 0 ? void 0 : config.core.dangerMode.get()) !== null && _a !== void 0 ? _a : false;
        return `${editSummary} ([[Wikipedia:Deputy|Deputy]] v${version}${dangerMode ? '!' : ''})`;
    }

    let InternalDeputyReviewDialog;
    /**
     * Initializes the process dialog.
     */
    function initDeputyReviewDialog() {
        var _a;
        InternalDeputyReviewDialog = (_a = class DeputyReviewDialog extends OO.ui.ProcessDialog {
                /**
                 *
                 * @param config
                 */
                constructor(config) {
                    config.size = config.size || 'larger';
                    super(config);
                    this.data = config;
                }
                /**
                 * @return The body height of this dialog.
                 */
                getBodyHeight() {
                    return 500;
                }
                /**
                 *
                 * @param {...any} args
                 */
                initialize(...args) {
                    super.initialize.apply(this, args);
                    this.element = h_1("div", { style: {
                            display: 'flex',
                            flexDirection: 'column',
                            alignItems: 'center',
                            textAlign: 'center'
                        } },
                        h_1("div", { style: { marginBottom: '8px' } }, mw.msg('deputy.diff.load')),
                        unwrapWidget(new OO.ui.ProgressBarWidget({
                            classes: ['dp-review-progress'],
                            progress: false
                        })));
                    this.content = new OO.ui.PanelLayout({ expanded: true, padded: true });
                    unwrapWidget(this.content).appendChild(this.element);
                    this.$body.append(this.content.$element);
                    return this;
                }
                /**
                 * @param data
                 * @return The ready process for this object.
                 */
                getReadyProcess(data) {
                    return super.getReadyProcess.call(this, data)
                        .next(new Promise((res) => {
                        // Load MediaWiki diff styles
                        mw.loader.using('mediawiki.diff.styles', () => res());
                    }))
                        .next(() => __awaiter(this, void 0, void 0, function* () {
                        // Load diff HTML
                        const compareRequest = yield MwApi.action.post({
                            action: 'compare',
                            fromtitle: this.data.title.getPrefixedText(),
                            fromslots: 'main',
                            totitle: this.data.title.getPrefixedText(),
                            toslots: 'main',
                            topst: 1,
                            prop: 'diff',
                            slots: 'main',
                            'fromtext-main': this.data.from,
                            'fromcontentformat-main': 'text/x-wiki',
                            'fromcontentmodel-main': 'wikitext',
                            'totext-main': this.data.to,
                            'tocontentformat-main': 'text/x-wiki',
                            'tocontentmodel-main': 'wikitext'
                        });
                        if (compareRequest.error) {
                            swapElements(this.element, unwrapWidget(new OO.ui.MessageWidget({
                                type: 'error',
                                label: mw.msg('deputy.diff.error')
                            })));
                        }
                        const diffHTML = compareRequest.compare.bodies.main;
                        if (!diffHTML) {
                            this.element = swapElements(this.element, h_1("div", { style: { textAlign: 'center' } }, mw.msg('deputy.diff.no-changes')));
                        }
                        else {
                            // noinspection JSXDomNesting
                            this.element = swapElements(this.element, h_1("table", { class: "diff" },
                                h_1("colgroup", null,
                                    h_1("col", { class: "diff-marker" }),
                                    h_1("col", { class: "diff-content" }),
                                    h_1("col", { class: "diff-marker" }),
                                    h_1("col", { class: "diff-content" })),
                                h_1("tbody", { dangerouslySetInnerHTML: diffHTML })));
                        }
                    }), this);
                }
                /**
                 * @param action
                 * @return The action process
                 */
                getActionProcess(action) {
                    if (action === 'close') {
                        return new OO.ui.Process(function () {
                            this.close({
                                action: action
                            });
                        }, this);
                    }
                    // Fallback to parent handler
                    return super.getActionProcess.call(this, action);
                }
            },
            _a.static = Object.assign(Object.assign({}, OO.ui.ProcessDialog.static), { name: 'deputyReviewDialog', title: mw.msg('deputy.diff'), actions: [
                    {
                        flags: ['safe', 'close'],
                        icon: 'close',
                        label: mw.msg('deputy.ante.close'),
                        title: mw.msg('deputy.ante.close'),
                        invisibleLabel: true,
                        action: 'close'
                    }
                ] }),
            _a);
    }
    /**
     * Creates a new DeputyReviewDialog.
     *
     * @param config
     * @return A DeputyReviewDialog
     */
    function DeputyReviewDialog (config) {
        if (!InternalDeputyReviewDialog) {
            initDeputyReviewDialog();
        }
        return new InternalDeputyReviewDialog(config);
    }

    /**
     * Get the content of a page on-wiki.
     *
     * @param page The page to get
     * @param extraOptions Extra options to pass to the request
     * @param api The API object to use
     * @return A promise resolving to the page content. Resolves to `null` if missing page.
     */
    function getPageContent (page, extraOptions = {}, api = MwApi.action) {
        return api.get(Object.assign(Object.assign(Object.assign({ action: 'query', prop: 'revisions' }, (typeof page === 'number' ? {
            pageids: page
        } : {
            titles: normalizeTitle(page).getPrefixedText()
        })), { rvprop: 'ids|content', rvslots: 'main', rvlimit: '1' }), extraOptions)).then((data) => {
            const fallbackText = extraOptions.fallbacktext;
            if (data.query.pages[0].revisions == null) {
                if (fallbackText) {
                    return Object.assign(fallbackText, {
                        page: data.query.pages[0]
                    });
                }
                else {
                    return null;
                }
            }
            return Object.assign(data.query.pages[0].revisions[0].slots.main.content, {
                contentFormat: data.query.pages[0].revisions[0].slots.main.contentformat,
                revid: data.query.pages[0].revisions[0].revid,
                page: data.query.pages[0]
            });
        });
    }

    /**
     * Opens a temporary window. Use this for dialogs that are immediately destroyed
     * after running. Do NOT use this for re-openable dialogs, such as the main ANTE
     * dialog.
     *
     * @param window
     * @return A promise. Resolves when the window is closed.
     */
    function openWindow(window) {
        return __awaiter(this, void 0, void 0, function* () {
            return new Promise((res) => {
                let wm = new OO.ui.WindowManager();
                document.getElementsByTagName('body')[0].appendChild(unwrapWidget(wm));
                wm.addWindows([window]);
                wm.openWindow(window);
                wm.on('closing', (win, closed) => {
                    closed.then(() => {
                        if (wm) {
                            const _wm = wm;
                            wm = null;
                            removeElement(unwrapWidget(_wm));
                            _wm.destroy();
                            res();
                        }
                    });
                });
            });
        });
    }

    /**
     * Automatically applies a change tag to edits made by the user if
     * a change tag was provided in the configuration.
     *
     * @param config
     * @return A spreadable Object containing the `tags` parameter for `action=edit`.
     */
    function changeTag(config) {
        return config.core.changeTag.get() ?
            { tags: config.core.changeTag.get() } :
            {};
    }

    let InternalCopiedTemplateEditorDialog;
    /**
     * Initializes the process element.
     */
    function initCopiedTemplateEditorDialog() {
        var _a;
        InternalCopiedTemplateEditorDialog = (_a = class CopiedTemplateEditorDialog extends OO.ui.ProcessDialog {
                /**
                 * @param config
                 */
                constructor(config) {
                    super(config);
                    /**
                     * Parsoid document for this dialog.
                     */
                    this.parsoid = new CTEParsoidDocument();
                    /**
                     * A map of OOUI PageLayouts keyed by their notices. These PageLayouts also include
                     * functions specific to AttributionNoticePageLayout, such as functions to get child
                     * pages.
                     */
                    this.pageCache = new Map();
                    this.main = config.main;
                }
                /**
                 * @return The body height of this dialog.
                 */
                getBodyHeight() {
                    return 900;
                }
                /**
                 * Initializes the dialog.
                 */
                initialize() {
                    super.initialize();
                    this.layout = new OO.ui.BookletLayout({
                        continuous: true,
                        outlined: true
                    });
                    this.layout.on('remove', () => {
                        if (Object.keys(this.layout.pages).length === 0) {
                            // If no pages left, append the "no notices" page.
                            this.layout.addPages([CopiedTemplatesEmptyPage({
                                    parent: this,
                                    parsoid: this.parsoid
                                })], 0);
                        }
                    });
                    this.parsoid.addEventListener('templateInsert', (event) => {
                        if (!this.pageCache.has(event.template)) {
                            this.pageCache.set(event.template, event.template.generatePage(this));
                            this.rebuildPages();
                        }
                    });
                    this.renderMenuActions();
                    this.$body.append(this.layout.$element);
                    return this;
                }
                /**
                 * Rebuilds the pages of this dialog.
                 */
                rebuildPages() {
                    const notices = this.parsoid.findNotices();
                    const pages = [];
                    for (const notice of notices) {
                        let cachedPage = this.pageCache.get(notice);
                        if (cachedPage == null) {
                            cachedPage = notice.generatePage(this);
                            this.pageCache.set(notice, cachedPage);
                        }
                        pages.push(cachedPage);
                        if (cachedPage.getChildren != null) {
                            pages.push(...cachedPage.getChildren());
                        }
                    }
                    const lastFocusedPage = this.layout.getCurrentPage();
                    let nextFocusedPageName;
                    const layoutPages = getObjectValues(this.layout.pages);
                    const removed = layoutPages
                        .filter((item) => pages.indexOf(item) === -1);
                    if (removed.indexOf(lastFocusedPage) === -1) {
                        // Focus on an existing (and currently focused) page.
                        nextFocusedPageName = this.layout.getCurrentPageName();
                    }
                    else if (lastFocusedPage != null && pages.length > 0) {
                        const layoutNames = Object.keys(this.layout.pages);
                        // Find the next page AFTER the one previously focused on (which is
                        // about to get removed).
                        for (let i = layoutNames.indexOf(lastFocusedPage.getName()); i < layoutNames.length; i++) {
                            const layoutName = layoutNames[i];
                            if (removed.some((p) => p.getName() !== layoutName)) {
                                // This element will not get removed later on. Use it.
                                nextFocusedPageName = layoutName;
                                break;
                            }
                        }
                        if (nextFocusedPageName == null) {
                            // Fall back to last element in the set (most likely position)
                            nextFocusedPageName = last(pages).getName();
                        }
                    }
                    // Remove all removed pages
                    this.layout.removePages(removed);
                    // Jank, but no other options while page rearranging isn't a thing.
                    this.layout.addPages(pages);
                    if (nextFocusedPageName) {
                        this.layout.setPage(nextFocusedPageName);
                    }
                    // Delete deleted pages from cache.
                    this.pageCache.forEach((page, notice) => {
                        if (removed.indexOf(page) !== -1) {
                            this.pageCache.delete(notice);
                        }
                    });
                }
                /**
                 * Renders the collection of actions at the top of the page menu. Also
                 * appends the panel to the layout.
                 */
                renderMenuActions() {
                    const addButton = new OO.ui.ButtonWidget({
                        icon: 'add',
                        framed: false,
                        invisibleLabel: true,
                        label: mw.msg('deputy.ante.add'),
                        title: mw.msg('deputy.ante.add'),
                        flags: ['progressive']
                    });
                    this.mergeButton = new OO.ui.ButtonWidget({
                        icon: 'tableMergeCells',
                        framed: false,
                        invisibleLabel: true,
                        label: mw.msg('deputy.ante.mergeAll'),
                        title: mw.msg('deputy.ante.mergeAll')
                    });
                    this.mergeButton.on('click', () => {
                        const notices = this.parsoid.findRowedNoticesByHref();
                        const noticeCount = Object.values(notices)
                            .filter(v => v.length > 1)
                            .reduce((p, n) => p + n.length, 0);
                        return noticeCount ?
                            dangerModeConfirm(window.CopiedTemplateEditor.config, mw.message('deputy.ante.mergeAll.confirm', `${noticeCount}`).text()).done((confirmed) => {
                                if (!confirmed) {
                                    return;
                                }
                                for (const noticeSet of Object.values(notices)) {
                                    TemplateMerger.merge(noticeSet);
                                }
                            }) :
                            OO.ui.alert('There are no templates to merge.');
                    });
                    const resetButton = new OO.ui.ButtonWidget({
                        icon: 'reload',
                        framed: false,
                        invisibleLabel: true,
                        label: mw.msg('deputy.ante.reset'),
                        title: mw.msg('deputy.ante.reset')
                    });
                    resetButton.on('click', () => {
                        dangerModeConfirm(window.CopiedTemplateEditor.config, mw.msg('deputy.ante.reset.confirm')).done((confirmed) => {
                            if (confirmed) {
                                this.loadTalkPage().then(() => {
                                    this.layout.clearPages();
                                    this.rebuildPages();
                                });
                            }
                        });
                    });
                    const deleteButton = new OO.ui.ButtonWidget({
                        icon: 'trash',
                        framed: false,
                        invisibleLabel: true,
                        label: mw.msg('deputy.ante.delete'),
                        title: mw.msg('deputy.ante.delete'),
                        flags: ['destructive']
                    });
                    deleteButton.on('click', () => {
                        // Original copied notice count.
                        const notices = this.parsoid.findNotices();
                        dangerModeConfirm(window.CopiedTemplateEditor.config, mw.message('deputy.ante.delete.confirm', `${notices.length}`).text()).done((confirmed) => {
                            if (confirmed) {
                                for (const notice of notices) {
                                    notice.destroy();
                                }
                            }
                        });
                    });
                    const previewButton = new OO.ui.ButtonWidget({
                        icon: 'eye',
                        framed: false,
                        invisibleLabel: true,
                        label: mw.msg('deputy.ante.preview'),
                        title: mw.msg('deputy.ante.preview'),
                        flags: ['destructive']
                    });
                    previewButton.on('click', () => __awaiter(this, void 0, void 0, function* () {
                        previewButton.setDisabled(true);
                        yield openWindow(DeputyReviewDialog({
                            title: normalizeTitle(this.parsoid.getPage()),
                            from: yield getPageContent(this.parsoid.getPage()),
                            to: yield this.parsoid.toWikitext()
                        }));
                        previewButton.setDisabled(false);
                    }));
                    this.layout.on('remove', () => {
                        this.mergeButton.setDisabled(!Object.values(this.parsoid.findRowedNoticesByHref())
                            .some(v => v.length > 1));
                        deleteButton.setDisabled(this.parsoid.findNotices().length === 0);
                    });
                    this.parsoid.addEventListener('templateInsert', () => {
                        this.mergeButton.setDisabled(!Object.values(this.parsoid.findRowedNoticesByHref())
                            .some(v => v.length > 1));
                        deleteButton.setDisabled(this.parsoid.findNotices().length === 0);
                    });
                    this.$overlay.append(new AttributionNoticeAddMenu(this.parsoid, addButton).render());
                    const actionPanel = h_1("div", { class: "cte-actionPanel" },
                        unwrapWidget(addButton),
                        unwrapWidget(this.mergeButton),
                        unwrapWidget(resetButton),
                        unwrapWidget(deleteButton),
                        unwrapWidget(previewButton));
                    const targetPanel = unwrapWidget(this.layout).querySelector('.oo-ui-menuLayout .oo-ui-menuLayout-menu');
                    targetPanel.insertAdjacentElement('afterbegin', actionPanel);
                }
                /**
                 * Loads the talk page.
                 */
                loadTalkPage() {
                    return __awaiter(this, void 0, void 0, function* () {
                        const talkPage = new mw.Title(mw.config.get('wgPageName'))
                            .getTalkPage()
                            .getPrefixedText();
                        // Load the talk page
                        yield this.parsoid.loadPage(talkPage, { reload: true })
                            .catch(errorToOO)
                            .then(() => true);
                        if (this.parsoid.getPage() !== talkPage) {
                            // Ask for user confirmation.
                            dangerModeConfirm(window.CopiedTemplateEditor.config, mw.message('deputy.ante.loadRedirect.message', talkPage, this.parsoid.getPage()).text(), {
                                title: mw.msg('deputy.ante.loadRedirect.title'),
                                actions: [
                                    {
                                        action: 'accept',
                                        label: mw.msg('deputy.ante.loadRedirect.source')
                                    },
                                    {
                                        action: 'deny',
                                        label: mw.msg('deputy.ante.loadRedirect.target')
                                    }
                                ]
                            }).then((loadSource) => {
                                if (loadSource) {
                                    // Load redirect page.
                                    return this.parsoid.loadPage(talkPage, { followRedirects: false, reload: true }).catch(errorToOO).then(() => true);
                                }
                            });
                        }
                    });
                }
                /**
                 * Gets the setup process for this dialog. This is run prior to the dialog
                 * opening.
                 *
                 * @param data Additional data. Unused for this dialog.
                 * @return An OOUI Process
                 */
                getSetupProcess(data) {
                    const process = super.getSetupProcess(data);
                    // Load the talk page
                    if (this.parsoid.getDocument() == null) {
                        process.next(this.loadTalkPage());
                    }
                    // Rebuild the list of pages
                    process.next(() => {
                        this.rebuildPages();
                        return true;
                    });
                    // Block exits
                    process.next(() => {
                        blockExit('cte');
                        return true;
                    });
                    return process;
                }
                /**
                 * Gets this dialog's ready process. Called after the dialog has opened.
                 *
                 * @return An OOUI Process
                 */
                getReadyProcess() {
                    const process = super.getReadyProcess();
                    // Recheck state of merge button
                    this.mergeButton.setDisabled(!Object.values(this.parsoid.findRowedNoticesByHref())
                        .some(v => v.length > 1));
                    process.next(() => {
                        for (const page of getObjectValues(this.layout.pages)) {
                            // Dirty check to see if this is a CopiedTemplatePage.
                            if (page.updatePreview != null) {
                                page.updatePreview();
                            }
                        }
                    }, this);
                    return process;
                }
                /**
                 * Gets this dialog's action process. Handles all actions (primarily dialog
                 * button clicks, etc.)
                 *
                 * @param action
                 * @return An OOUI Process
                 */
                getActionProcess(action) {
                    const process = super.getActionProcess(action);
                    if (action === 'save') {
                        // Quick and dirty validity check.
                        if (unwrapWidget(this.layout)
                            .querySelector('.oo-ui-flaggedElement-invalid') != null) {
                            return new OO.ui.Process(() => {
                                OO.ui.alert(mw.msg('deputy.ante.invalid'));
                            });
                        }
                        // Saves the page.
                        process.next(() => __awaiter(this, void 0, void 0, function* () {
                            return new mw.Api().postWithEditToken(Object.assign(Object.assign({}, changeTag(yield window.CopiedTemplateEditor.getWikiConfig())), { action: 'edit', format: 'json', formatversion: '2', utf8: 'true', title: this.parsoid.getPage(), text: yield this.parsoid.toWikitext(), summary: decorateEditSummary(mw.msg(this.parsoid.originalCount > 0 ?
                                    'deputy.ante.content.modify' :
                                    'deputy.ante.content.add'), window.CopiedTemplateEditor.config) })).catch((e, c) => {
                                throw errorToOO(e, c);
                            });
                        }), this);
                        // Page redirect
                        process.next(() => {
                            unblockExit('cte');
                            if (mw.config.get('wgPageName') === this.parsoid.getPage()) {
                                // If on the talk page, reload the page.
                                window.location.reload();
                            }
                            else {
                                // If on another page, open the talk page.
                                window.location.href =
                                    mw.config.get('wgArticlePath').replace(/\$1/g, encodeURIComponent(this.parsoid.getPage()));
                            }
                        }, this);
                    }
                    process.next(() => {
                        this.close({ action: action });
                    }, this);
                    return process;
                }
                /**
                 * Gets the teardown process. Called when the dialog is closing.
                 *
                 * @return An OOUI process.
                 */
                getTeardownProcess() {
                    const process = super.getTeardownProcess();
                    process.next(() => {
                        // Already unblocked if "save", but this cuts down on code footprint.
                        unblockExit('cte');
                        this.main.toggleButtons(true);
                    });
                    return process;
                }
            },
            _a.static = Object.assign(Object.assign({}, OO.ui.ProcessDialog.static), { name: 'copiedTemplateEditorDialog', title: mw.msg('deputy.ante'), size: 'huge', actions: [
                    {
                        flags: ['primary', 'progressive'],
                        label: mw.msg('deputy.save'),
                        title: mw.msg('deputy.save'),
                        action: 'save'
                    },
                    {
                        flags: ['safe', 'close'],
                        icon: 'close',
                        label: mw.msg('deputy.close'),
                        title: mw.msg('deputy.close'),
                        invisibleLabel: true,
                        action: 'close'
                    }
                ] }),
            _a);
    }
    /**
     * Creates a new CopiedTemplateEditorDialog.
     *
     * @param config
     * @return A CopiedTemplateEditorDialog object
     */
    function CopiedTemplateEditorDialog (config) {
        if (!InternalCopiedTemplateEditorDialog) {
            initCopiedTemplateEditorDialog();
        }
        return new InternalCopiedTemplateEditorDialog(config);
    }

    var deputyAnteEnglish = {
    	"deputy.ante.content.modify": "Modifying content attribution notices.",
    	"deputy.ante.content.add": "Adding content attribution notices.",
    	"deputy.ante.edit": "Modify content attribution notices for this page",
    	"deputy.ante.add": "Add a notice",
    	"deputy.ante.mergeAll": "Merge all notices",
    	"deputy.ante.mergeAll.confirm": "You are about to merge $1 {{PLURAL:$1|notice|notices}} which support rows. Continue?",
    	"deputy.ante.reset": "Reset all changes",
    	"deputy.ante.reset.confirm": "This will reset all changes. Proceed?",
    	"deputy.ante.delete": "Delete all notices",
    	"deputy.ante.delete.confirm": "You are about to delete $1 {{PLURAL:$1|notice|notices}}. Continue?",
    	"deputy.ante.preview": "Preview changes",
    	"deputy.ante.loadRedirect.title": "Redirected talk page",
    	"deputy.ante.loadRedirect.message": "The talk page for \"$1\" currently redirects to \"$2\". Which page should be used for modifying attribution notices?",
    	"deputy.ante.loadRedirect.source": "Source (redirect) page",
    	"deputy.ante.loadRedirect.target": "Target page",
    	"deputy.ante.nocat.head": "<code>nocat</code> is enabled",
    	"deputy.ante.nocat.help": "This notice has the <code>nocat</code> parameter enabled and, as an effect, is not being tracked in categories. This usually means that the template is for demonstration purposes only.",
    	"deputy.ante.nocat.clear": "Restore tracking",
    	"deputy.ante.demo.head": "<code>demo</code> is enabled",
    	"deputy.ante.demo.help": "This notice has the <code>demo</code> parameter enabled. This usually means that the template is for demonstration purposes only.",
    	"deputy.ante.demo.clear": "Clear demo mode",
    	"deputy.ante.invalid": "Some fields are still invalid.",
    	"deputy.ante.adding": "Adding content attribution notices",
    	"deputy.ante.modifying": "Modifying content attribution notices",
    	"deputy.ante.dirty": "This dialog did not close properly last time. Your changes will be reset.",
    	"deputy.ante.empty.header": "No notices",
    	"deputy.ante.empty.removed": "All notices will be removed from the page. To reset your changes and restore previous templates, press the reset button at the bottom of the dialog.",
    	"deputy.ante.empty.none": "There are currently no notices on the talk page.",
    	"deputy.ante.empty.add": "Add a notice",
    	"deputy.ante.noSpot": "Sorry, but a notice cannot be automatically added. Please contact the developer to possibly add support for this talk page.",
    	"deputy.ante.merge": "Merge",
    	"deputy.ante.merge.title": "Merge notices",
    	"deputy.ante.merge.from.label": "Notices to merge",
    	"deputy.ante.merge.from.select": "Select a notice",
    	"deputy.ante.merge.from.empty": "No notices to merge",
    	"deputy.ante.merge.all": "Merge all",
    	"deputy.ante.merge.all.confirm": "You are about to merge $1 'copied' {{PLURAL:$1|notice|notices}} into this notice. Continue?",
    	"deputy.ante.merge.button": "Merge",
    	"deputy.ante.templateOptions": "Template options",
    	"deputy.ante.dateAuto": "Pull the date from the provided revision ID (`$1` parameter)",
    	"deputy.ante.dateAuto.invalid": "Parameter does not appear to be a valid revision ID.",
    	"deputy.ante.dateAuto.failed": "Could not pull date from revision: $1",
    	"deputy.ante.dateAuto.missing": "The revision $1 could not be found. Its page may have been deleted.",
    	"deputy.ante.revisionAuto": "Latest",
    	"deputy.ante.revisionAuto.title": "Pull the revision ID from the latest (current) revision of the page in `$1`.",
    	"deputy.ante.revisionAuto.failed": "Could not pull revision ID from page: $1",
    	"deputy.ante.revisionAuto.missing": "The page $1 could not be found. It may have been deleted.",
    	"deputy.ante.copied.label": "Copied $1",
    	"deputy.ante.copied.remove": "Remove notice",
    	"deputy.ante.copied.remove.confirm": "This will destroy $1 {{PLURAL:$1|entry|entries}}. Continue?",
    	"deputy.ante.copied.add": "Add entry",
    	"deputy.ante.copied.entry.label": "Template entry",
    	"deputy.ante.copied.entry.short": "$1 to $2",
    	"deputy.ante.copied.entry.shortTo": "To $1",
    	"deputy.ante.copied.entry.shortFrom": "From $1",
    	"deputy.ante.copied.entry.remove": "Remove entry",
    	"deputy.ante.copied.entry.copy": "Copy attribution edit summary",
    	"deputy.ante.copied.entry.copy.lacking": "Attribution edit summary copied to clipboard with lacking properties. Ensure that `from` is supplied.",
    	"deputy.ante.copied.entry.copy.success": "Attribution edit summary copied to clipboard.",
    	"deputy.ante.copied.collapse": "Collapse",
    	"deputy.ante.copied.small": "Small",
    	"deputy.ante.copied.convert": "Convert",
    	"deputy.ante.copied.from.placeholder": "Page A",
    	"deputy.ante.copied.from.label": "Page copied from",
    	"deputy.ante.copied.from.help": "This is the page from which the content was copied from.",
    	"deputy.ante.copied.from_oldid.placeholder": "from_oldid",
    	"deputy.ante.copied.from_oldid.label": "Revision ID",
    	"deputy.ante.copied.from_oldid.help": "The specific revision ID at the time that the content was copied, if known.",
    	"deputy.ante.copied.to.placeholder": "Page B",
    	"deputy.ante.copied.to.label": "Page copied to",
    	"deputy.ante.copied.to.help": "This is the page where the content was copied into.",
    	"deputy.ante.copied.to_diff.placeholder": "to_diff",
    	"deputy.ante.copied.to_diff.label": "Revision ID",
    	"deputy.ante.copied.to_diff.help": "The specific revision ID of the revision that copied content into the target page. If the copying spans multiple revisions, this is the ID of the last revision that copies content into the page.",
    	"deputy.ante.copied.to_oldid.placeholder": "to_oldid",
    	"deputy.ante.copied.to_oldid.label": "Starting revision ID",
    	"deputy.ante.copied.to_oldid.help": "The ID of the revision before any content was copied. This can be omitted unless multiple revisions copied content into the page.",
    	"deputy.ante.copied.diff.placeholder": "https://en.wikipedia.org/w/index.php?diff=123456",
    	"deputy.ante.copied.diff.label": "Diff URL",
    	"deputy.ante.copied.diff.help": "The URL of the diff. Using <code>to_diff</code> and <code>to_oldid</code> is preferred, although supplying this parameter will override both.",
    	"deputy.ante.copied.merge.label": "Merged?",
    	"deputy.ante.copied.merge.help": "Whether the copying was done as a result of merging two pages.",
    	"deputy.ante.copied.afd.placeholder": "AfD page (without Wikipedia:Articles for deletion/)",
    	"deputy.ante.copied.afd.label": "AfD page",
    	"deputy.ante.copied.afd.help": "The AfD page if the copy was made due to an AfD closed as \"merge\".",
    	"deputy.ante.copied.date.placeholder": "Date (YYYY-MM-DD)",
    	"deputy.ante.copied.advanced": "Advanced",
    	"deputy.ante.copied.dateInvalid": "The previous date value, \"$1\", was not a valid date.",
    	"deputy.ante.copied.diffDeprecate": "The <code>to_diff</code> and <code>to_oldid</code> parameters are preferred in favor of the <code>diff</code> parameter.",
    	"deputy.ante.copied.diffDeprecate.warnHost": "The URL in this parameter is not the same as the wiki you're currently editing on. Continue?",
    	"deputy.ante.copied.diffDeprecate.replace": "The current value of '$1', \"$2\", will be replaced with \"$3\". Continue?",
    	"deputy.ante.copied.diffDeprecate.failed": "Cannot convert `diff` parameter to URL. See your browser console for more details.",
    	"deputy.ante.splitArticle.label": "Split article $1",
    	"deputy.ante.splitArticle.remove": "Remove notice",
    	"deputy.ante.splitArticle.remove.confirm": "This will destroy $1 {{PLURAL:$1|entry|entries}}. Continue?",
    	"deputy.ante.splitArticle.add": "Add entry",
    	"deputy.ante.splitArticle.entry.label": "Template entry",
    	"deputy.ante.splitArticle.entry.remove": "Remove entry",
    	"deputy.ante.splitArticle.entry.short": "$1 on $2",
    	"deputy.ante.splitArticle.collapse": "Collapse",
    	"deputy.ante.splitArticle.from": "From",
    	"deputy.ante.splitArticle.from.help": "This is the page where the content was split from. In most cases, this is the current page, and can be left blank.",
    	"deputy.ante.splitArticle.to.placeholder": "Subpage A",
    	"deputy.ante.splitArticle.to.label": "Page split to",
    	"deputy.ante.splitArticle.to.help": "This is the name of page that material was copied to; the \"merge target\".",
    	"deputy.ante.splitArticle.from_oldid.placeholder": "from_oldid",
    	"deputy.ante.splitArticle.from_oldid.label": "As of revision ID",
    	"deputy.ante.splitArticle.from_oldid.help": "The revision ID of the original page prior to the split. This is the revision that still contains content that will eventually become part of the split, with the following revision (or succeeding revisions) progressively transferring content to the other pages.",
    	"deputy.ante.splitArticle.date.label": "Date of split",
    	"deputy.ante.splitArticle.date.help": "The date that the split occurred.",
    	"deputy.ante.splitArticle.diff.placeholder": "123456789",
    	"deputy.ante.splitArticle.diff.label": "Diff",
    	"deputy.ante.splitArticle.diff.help": "The diff URL or revision ID of the split.",
    	"deputy.ante.mergedFrom.label": "Merged from $1",
    	"deputy.ante.mergedFrom.remove": "Remove notice",
    	"deputy.ante.mergedFrom.article.placeholder": "Page A",
    	"deputy.ante.mergedFrom.article.label": "Original article",
    	"deputy.ante.mergedFrom.article.help": "This is the page where the merged content is or used to be.",
    	"deputy.ante.mergedFrom.date.label": "Date",
    	"deputy.ante.mergedFrom.date.help": "The date (in UTC) when the content was merged into this page.",
    	"deputy.ante.mergedFrom.talk.label": "Link to talk?",
    	"deputy.ante.mergedFrom.talk.help": "Whether to link to the original article's talk page or not.",
    	"deputy.ante.mergedFrom.target.placeholder": "Page B",
    	"deputy.ante.mergedFrom.target.label": "Merge target",
    	"deputy.ante.mergedFrom.target.help": "The page that the content was merged into. Used if the page that the content was merged into was the talk page.",
    	"deputy.ante.mergedFrom.afd.placeholder": "Wikipedia:Articles for deletion/Page A",
    	"deputy.ante.mergedFrom.afd.label": "AfD",
    	"deputy.ante.mergedFrom.afd.help": "The AfD discussion that led to the merge. If this merge was not the result of an AfD discussion, leave this blank.",
    	"deputy.ante.mergedTo.label": "Merged to $1",
    	"deputy.ante.mergedTo.remove": "Remove notice",
    	"deputy.ante.mergedTo.to.placeholder": "Page A",
    	"deputy.ante.mergedTo.to.label": "Target article",
    	"deputy.ante.mergedTo.to.help": "This is the page where content was copied into.",
    	"deputy.ante.mergedTo.date.label": "Date",
    	"deputy.ante.mergedTo.date.help": "The date (in UTC) when the content was merged into this page.",
    	"deputy.ante.mergedTo.small.label": "Small",
    	"deputy.ante.mergedTo.small.help": "If enabled, makes the banner small.",
    	"deputy.ante.backwardsCopy.label": "Backwards copy $1",
    	"deputy.ante.backwardsCopy.remove": "Remove notice",
    	"deputy.ante.backwardsCopy.bot": "This notice was automatically added in by [[User:$1|$1]] ([[User talk:$1|talk]]). Changing this template will remove this warning as it is assumed that you have properly vetted the bot-added parameters.",
    	"deputy.ante.backwardsCopy.entry.label": "Template entry",
    	"deputy.ante.backwardsCopy.entry.short": "Copied in '$1'",
    	"deputy.ante.backwardsCopy.entry.remove": "Remove entry",
    	"deputy.ante.backwardsCopy.comments.placeholder": "Additional information",
    	"deputy.ante.backwardsCopy.comments.label": "Comments",
    	"deputy.ante.backwardsCopy.comments.help": "Additional comments related to the backwards copies.",
    	"deputy.ante.backwardsCopy.id.placeholder": "123456789",
    	"deputy.ante.backwardsCopy.id.label": "Revision ID",
    	"deputy.ante.backwardsCopy.id.help": "The last revision ID of this article that does not contain content that was duplicated by copying media.",
    	"deputy.ante.backwardsCopy.entry.title.placeholder": "Article, journal, or medium name",
    	"deputy.ante.backwardsCopy.entry.title.label": "Publication name",
    	"deputy.ante.backwardsCopy.entry.title.help": "The publication title. This is the title of the medium that copied from Wikipedia",
    	"deputy.ante.backwardsCopy.entry.date.placeholder": "12 Feburary 2022",
    	"deputy.ante.backwardsCopy.entry.date.label": "Publishing date",
    	"deputy.ante.backwardsCopy.entry.date.help": "This is the date on which the article was first published.",
    	"deputy.ante.backwardsCopy.entry.author.placeholder": "Add author",
    	"deputy.ante.backwardsCopy.entry.author.label": "Author",
    	"deputy.ante.backwardsCopy.entry.author.help": "The article's author.",
    	"deputy.ante.backwardsCopy.entry.url.placeholder": "https://example.com/news/a-news-article-that-copies-from-wikipedia",
    	"deputy.ante.backwardsCopy.entry.url.label": "URL",
    	"deputy.ante.backwardsCopy.entry.url.help": "A URL to the published media, if it exists as an online resource. If this is not an online resource (newspaper media, other printed media), leave this blank.",
    	"deputy.ante.backwardsCopy.entry.org.placeholder": "Example Publishing",
    	"deputy.ante.backwardsCopy.entry.org.label": "Publisher",
    	"deputy.ante.backwardsCopy.entry.org.help": "The publisher of the media. This may be a news company or a book publishing company.",
    	"deputy.ante.translatedPage.label": "Translated from $1:$2",
    	"deputy.ante.translatedPage.remove": "Remove notice",
    	"deputy.ante.translatedPage.lang.placeholder": "en, de, fr, es, etc.",
    	"deputy.ante.translatedPage.lang.label": "Language code",
    	"deputy.ante.translatedPage.lang.help": "The language code of the wiki that the page was translated from. This is the \"en\" of the English Wikipedia, or the \"fr\" of the French Wikipedia.",
    	"deputy.ante.translatedPage.page.placeholder": "Page on other wiki",
    	"deputy.ante.translatedPage.page.label": "Source page",
    	"deputy.ante.translatedPage.page.help": "The page on the other wiki that the content was copied from. Do not translate the page title.",
    	"deputy.ante.translatedPage.comments.placeholder": "Additional comments",
    	"deputy.ante.translatedPage.comments.label": "Comments",
    	"deputy.ante.translatedPage.comments.help": "Additional comments that are pertinent to translation.",
    	"deputy.ante.translatedPage.version.placeholder": "123456789",
    	"deputy.ante.translatedPage.version.label": "Source revision ID",
    	"deputy.ante.translatedPage.version.help": "The revision ID of the source page at the time of translation.",
    	"deputy.ante.translatedPage.insertversion.placeholder": "987654321",
    	"deputy.ante.translatedPage.insertversion.label": "Insertion revision ID",
    	"deputy.ante.translatedPage.insertversion.help": "The revision ID of the revision where the translated content was inserted into the page bearing this notice.",
    	"deputy.ante.translatedPage.section.placeholder": "Section name (leave blank if N/A)",
    	"deputy.ante.translatedPage.section.label": "Section",
    	"deputy.ante.translatedPage.section.help": "The section of the page that was translated, if a specific section was translated. Leave blank if this does not apply, or if translation was performed on the entire page or more than one section.",
    	"deputy.ante.translatedPage.small.label": "Small?",
    	"deputy.ante.translatedPage.small.help": "Whether to render the template as a small message box or not. By default, a small box is used. If you have a good reason to use a full-sized banner, disable this option.",
    	"deputy.ante.translatedPage.partial.label": "Partial?",
    	"deputy.ante.translatedPage.partial.help": "Whether this translation is a partial translation or not.",
    	"deputy.ante.translatedPage.copy": "Copy attribution edit summary",
    	"deputy.ante.translatedPage.copy.lacking": "Attribution edit summary copied to clipboard with lacking properties. Ensure that `from` is supplied.",
    	"deputy.ante.translatedPage.copy.success": "Attribution edit summary copied to clipboard."
    };

    /**
     * Handles resource fetching operations.
     */
    class DeputyResources {
        /**
         * Loads a resource from the provided resource root.
         *
         * @param path A path relative to the resource root.
         * @return A Promise that resolves to the resource's content as a UTF-8 string.
         */
        static loadResource(path) {
            return __awaiter(this, void 0, void 0, function* () {
                switch (this.root.type) {
                    case 'url': {
                        const headers = new Headers();
                        headers.set('Origin', window.location.origin);
                        return fetch((new URL(path, this.root.url)).href, {
                            method: 'GET',
                            headers
                        }).then((r) => r.text());
                    }
                    case 'wiki': {
                        this.assertApi();
                        return getPageContent(this.root.prefix.replace(/\/$/, '') + '/' + path, {}, this.api);
                    }
                }
            });
        }
        /**
         * Ensures that `this.api` is a valid ForeignApi.
         */
        static assertApi() {
            if (this.root.type !== 'wiki') {
                return;
            }
            if (!this.api) {
                this.api = new mw.ForeignApi(this.root.wiki.toString(), {
                    // Force anonymous mode. Deputy doesn't need user data anyway,
                    // so this should be fine.
                    anonymous: true
                });
            }
        }
    }
    /**
     * The root of all Deputy resources. This should serve static data that Deputy will
     * use to load resources such as language files.
     */
    DeputyResources.root = {
        type: 'url',
        url: new URL('https://tools-static.wmflabs.org/deputy/')
    };

    var _a;
    const USER_LOCALE = (_a = window.deputyLang) !== null && _a !== void 0 ? _a : mw.config.get('wgUserLanguage');

    /**
     * Handles internationalization and localization for Deputy and sub-modules.
     */
    class DeputyLanguage {
        /**
         * Loads the language for this Deputy interface.
         *
         * @param module The module to load a language pack for.
         * @param fallback A fallback language pack to load. Since this is an actual
         * `Record`, this relies on the language being bundled with the userscript. This ensures
         * that a language pack is always available, even if a language file could not be loaded.
         */
        static load(module, fallback) {
            var _a;
            return __awaiter(this, void 0, void 0, function* () {
                const lang = (_a = window.deputyLang) !== null && _a !== void 0 ? _a : 'en';
                // The loaded language resource file. Forced to `null` if using English, since English
                // is our fallback language.
                const langResource = lang === 'en' ? null :
                    yield DeputyResources.loadResource(`i18n/${module}/${lang}.json`)
                        .catch(() => {
                        // Could not find requested language file.
                        return null;
                    });
                if (!langResource) {
                    // Fall back.
                    for (const key in fallback) {
                        mw.messages.set(key, fallback[key]);
                    }
                    return;
                }
                try {
                    const langData = JSON.parse(langResource);
                    for (const key in langData) {
                        mw.messages.set(key, langData[key]);
                    }
                }
                catch (e) {
                    error(e);
                    mw.notify(
                    // No languages to fall back on. Do not translate this string.
                    'Deputy: Requested language page is not a valid JSON file.', { type: 'error' });
                    // Fall back.
                    for (const key in fallback) {
                        mw.messages.set(key, fallback[key]);
                    }
                }
                if (lang !== mw.config.get('wgUserLanguage')) {
                    yield DeputyLanguage.loadSecondary();
                }
            });
        }
        /**
         * Loads a specific moment.js locale. It's possible for nothing to be loaded (e.g. if the
         * locale is not supported by moment.js), in which case nothing happens and English is
         * likely used.
         *
         * @param locale The locale to load. `window.deputyLang` by default.
         */
        static loadMomentLocale(locale = USER_LOCALE) {
            return __awaiter(this, void 0, void 0, function* () {
                if (locale === 'en') {
                    // Always loaded.
                    return;
                }
                if (mw.loader.getState('moment') !== 'ready') {
                    // moment.js is not yet loaded.
                    warn('Deputy tried loading moment.js locales but moment.js is not yet ready.');
                    return;
                }
                if (window.moment.locales().indexOf(locale) !== -1) {
                    // Already loaded.
                    return;
                }
                yield mw.loader.using('moment')
                    .then(() => true, () => null);
                yield mw.loader.getScript(new URL(`resources/lib/moment/locale/${locale}.js`, new URL(mw.util.wikiScript('index'), window.location.href)).href).then(() => true, () => null);
            });
        }
        /**
         * There are times when the user interface language do not match the wiki content
         * language. Since Deputy's edit summary and content strings are found in the
         * i18n files, however, there are cases where the wrong language would be used.
         *
         * This solves this problem by manually overriding content-specific i18n keys with
         * the correct language. By default, all keys that match `deputy.*.content.**` get
         * overridden.
         *
         * There are no fallbacks for this. If it fails, the user interface language is
         * used anyway. In the event that the user interface language is not English,
         * this will cause awkward situations. Whether or not something should be done to
         * catch this specific edge case will depend on how frequent it happens.
         *
         * @param locale
         * @param match
         */
        static loadSecondary(locale = mw.config.get('wgContentLanguage'), match = /^deputy\.(?:[^.]+)?\.content\./g) {
            return __awaiter(this, void 0, void 0, function* () {
                // The loaded language resource file. Forced to `null` if using English, since English
                // is our fallback language.
                const langResource = locale === 'en' ? null :
                    yield DeputyResources.loadResource(`i18n/${module}/${locale}.json`)
                        .catch(() => {
                        // Could not find requested language file.
                        return null;
                    });
                if (!langResource) {
                    return;
                }
                try {
                    const langData = JSON.parse(langResource);
                    for (const key in langData) {
                        if (cloneRegex$1(match).test(key)) {
                            mw.messages.set(key, langData[key]);
                        }
                    }
                }
                catch (e) {
                    // Silent failure.
                    error('Deputy: Requested language page is not a valid JSON file.', e);
                }
            });
        }
    }

    var deputySharedEnglish = {
    	"deputy.name": "Deputy",
    	"deputy.description": "Copyright cleanup and case processing tool for Wikipedia.",
    	"deputy.ia": "Infringement Assistant",
    	"deputy.ia.short": "I. Assistant",
    	"deputy.ia.acronym": "Deputy: IA",
    	"deputy.ante": "Attribution Notice Template Editor",
    	"deputy.ante.short": "Attrib. Template Editor",
    	"deputy.ante.acronym": "Deputy: ANTE",
    	"deputy.cancel": "Cancel",
    	"deputy.review": "Review",
    	"deputy.review.title": "Review a diff of the changes to be made to the page",
    	"deputy.save": "Save",
    	"deputy.close": "Close",
    	"deputy.positiveDiff": "+{{FORMATNUM:$1}}",
    	"deputy.negativeDiff": "-{{FORMATNUM:$1}}",
    	"deputy.zeroDiff": "0",
    	"deputy.brokenDiff": "?",
    	"deputy.brokenDiff.explain": "The internal parent revision ID for this diff points to a non-existent revision. [[phab:T186280]] has more information.",
    	"deputy.moreInfo": "More information",
    	"deputy.dismiss": "Dismiss",
    	"deputy.revision.cur": "cur",
    	"deputy.revision.prev": "prev",
    	"deputy.revision.cur.tooltip": "Difference with latest revision",
    	"deputy.revision.prev.tooltip": "Difference with preceding revision",
    	"deputy.revision.talk": "talk",
    	"deputy.revision.contribs": "contribs",
    	"deputy.revision.bytes": "{{FORMATNUM:$1}} bytes",
    	"deputy.revision.byteChange": "{{FORMATNUM:$1}} bytes after change of this size",
    	"deputy.revision.tags": "{{PLURAL:$1|Tag|Tags}}:",
    	"deputy.revision.new": "N",
    	"deputy.revision.new.tooltip": "This edit created a new page.",
    	"deputy.comma-separator": ", ",
    	"deputy.diff": "Review your changes",
    	"deputy.diff.load": "Loading changes...",
    	"deputy.diff.no-changes": "No difference",
    	"deputy.diff.error": "An error occurred while trying to get the comparison.",
    	"deputy.loadError.userConfig": "Due to an error, your Deputy configuration has been reset.",
    	"deputy.loadError.wikiConfig": "An error occurred while loading this wiki's Deputy configuration. Please report this to the Deputy maintainers for this wiki."
    };

    /**
     * Refers to a specific setting on the configuration. Should be initialized with
     * a raw (serialized) type and an actual (deserialized) type.
     *
     * This is used for both client and wiki-wide configuration.
     */
    class Setting {
        /**
         *
         * @param options
         * @param options.serialize Serialization function. See {@link Setting#serialize}
         * @param options.deserialize Deserialization function. See {@link Setting#deserialize}
         * @param options.alwaysSave See {@link Setting#alwaysSave}.
         * @param options.defaultValue Default value. If not supplied, `undefined` is used.
         * @param options.displayOptions See {@link Setting#displayOptions}
         * @param options.allowedValues See {@link Setting#allowedValues}
         */
        constructor(options) {
            var _a, _b;
            this.serialize = options.serialize;
            this.deserialize = options.deserialize;
            this.displayOptions = options.displayOptions;
            this.allowedValues = options.allowedValues;
            this.value = this.defaultValue = options.defaultValue;
            this.alwaysSave = options.alwaysSave;
            this.isDisabled = ((_a = options.displayOptions) === null || _a === void 0 ? void 0 : _a.disabled) != null ?
                (typeof options.displayOptions.disabled === 'function' ?
                    options.displayOptions.disabled.bind(this) :
                    () => options.displayOptions.disabled) : () => false;
            this.isHidden = ((_b = options.displayOptions) === null || _b === void 0 ? void 0 : _b.hidden) != null ?
                (typeof options.displayOptions.hidden === 'function' ?
                    options.displayOptions.hidden.bind(this) :
                    () => options.displayOptions.hidden) : () => false;
        }
        /**
         * @return `true` if `this.value` is not null or undefined.
         */
        ok() {
            return this.value != null;
        }
        /**
         * @return The current value of this setting.
         */
        get() {
            return this.value;
        }
        /**
         * Sets the value and performs validation. If the input is an invalid value, and
         * `throwOnInvalid` is false, the value will be reset to default.
         *
         * @param v
         * @param throwOnInvalid
         */
        set(v, throwOnInvalid = false) {
            if (this.locked) {
                warn('Attempted to modify locked setting.');
                return;
            }
            if (this.allowedValues) {
                const keys = Array.isArray(this.allowedValues) ?
                    this.allowedValues : getObjectValues(this.allowedValues);
                if (Array.isArray(v)) {
                    if (v.some((v1) => keys.indexOf(v1) === -1)) {
                        if (throwOnInvalid) {
                            throw new Error('Invalid value');
                        }
                        v = this.value;
                    }
                }
                else {
                    if (this.allowedValues && keys.indexOf(v) === -1) {
                        if (throwOnInvalid) {
                            throw new Error('Invalid value');
                        }
                        v = this.value;
                    }
                }
            }
            this.value = v;
        }
        /**
         * Resets this setting to its original value.
         */
        reset() {
            this.set(this.defaultValue);
        }
        /**
         * Parses a given raw value and mutates the setting.
         *
         * @param raw The raw value to parse.
         * @return The new value.
         */
        load(raw) {
            return (this.value = this.deserialize(raw));
        }
        /**
         * Prevents the value of the setting from being changed. Used for debugging.
         */
        lock() {
            this.locked = true;
        }
        /**
         * Allows the value of the setting to be changed. Used for debugging.
         */
        unlock() {
            this.locked = false;
        }
    }
    Setting.basicSerializers = {
        serialize: (value) => value,
        deserialize: (value) => value
    };

    /**
     * Works like `Object.fromEntries`
     *
     * @param obj The object to get the values of.
     * @return The values of the given object as an array
     */
    function fromObjectEntries(obj) {
        const i = {};
        for (const [key, value] of obj) {
            i[key] = value;
        }
        return i;
    }

    /**
     * Generates serializer and deserializer for serialized <b>string</b> enums.
     *
     * Trying to use anything that isn't a string enum here (union enum, numeral enum)
     * will likely cause serialization/deserialization failures.
     *
     * @param _enum
     * @param defaultValue
     * @return An object containing a `serializer` and `deserializer`.
     */
    function generateEnumSerializers(_enum, defaultValue) {
        return {
            serialize: (value) => value === defaultValue ? undefined : value,
            deserialize: (value) => value
        };
    }
    /**
     * Generates configuration properties for serialized <b>string</b> enums.
     *
     * Trying to use anything that isn't a string enum here (union enum, numeral enum)
     * will likely cause serialization/deserialization failures.
     *
     * @param _enum
     * @param defaultValue
     * @return Setting properties.
     */
    function generateEnumConfigurationProperties(_enum, defaultValue) {
        return Object.assign(Object.assign({}, generateEnumSerializers(_enum, defaultValue)), { displayOptions: {
                type: 'radio'
            }, allowedValues: fromObjectEntries(Array.from(new Set(Object.keys(_enum)).values())
                .map((v) => [_enum[v], _enum[v]])), defaultValue: defaultValue });
    }
    var PortletNameView;
    (function (PortletNameView) {
        PortletNameView["Full"] = "full";
        PortletNameView["Short"] = "short";
        PortletNameView["Acronym"] = "acronym";
    })(PortletNameView || (PortletNameView = {}));

    var CompletionAction;
    (function (CompletionAction) {
        CompletionAction["Nothing"] = "nothing";
        CompletionAction["Reload"] = "reload";
    })(CompletionAction || (CompletionAction = {}));
    var TripleCompletionAction;
    (function (TripleCompletionAction) {
        TripleCompletionAction["Nothing"] = "nothing";
        TripleCompletionAction["Reload"] = "reload";
        TripleCompletionAction["Redirect"] = "redirect";
    })(TripleCompletionAction || (TripleCompletionAction = {}));

    /**
     * A configuration. Defines settings and setting groups.
     */
    class ConfigurationBase {
        // eslint-disable-next-line jsdoc/require-returns-check
        /**
         * @return the configuration from the current wiki.
         */
        static load() {
            throw new Error('Unimplemented method.');
        }
        /**
         * Creates a new Configuration.
         */
        constructor() { }
        /**
         * Deserializes a JSON configuration into this configuration. This WILL overwrite
         * past settings.
         *
         * @param serializedData
         */
        deserialize(serializedData) {
            var _a;
            for (const group in this.all) {
                const groupObject = this.all[group];
                for (const key in this.all[group]) {
                    const setting = groupObject[key];
                    if (((_a = serializedData === null || serializedData === void 0 ? void 0 : serializedData[group]) === null || _a === void 0 ? void 0 : _a[key]) !== undefined) {
                        setting.set(setting.deserialize ?
                            // Type-checked upon declaration, just trust it to skip errors.
                            setting.deserialize(serializedData[group][key]) :
                            serializedData[group][key]);
                    }
                }
            }
        }
        /**
         * @return the serialized version of the configuration. All `undefined` values are stripped
         * from output. If a category remains unchanged from defaults, it is skipped. If the entire
         * configuration remains unchanged, `null` is returned.
         */
        serialize() {
            const config = {};
            for (const group of Object.keys(this.all)) {
                const groupConfig = {};
                const groupObject = this.all[group];
                for (const key in this.all[group]) {
                    const setting = groupObject[key];
                    if (setting.get() === setting.defaultValue && !setting.alwaysSave) {
                        continue;
                    }
                    const serialized = setting.serialize ?
                        // Type-checked upon declaration, just trust it to skip errors.
                        setting.serialize(setting.get()) : setting.get();
                    if (serialized !== undefined) {
                        groupConfig[key] = serialized;
                    }
                }
                if (Object.keys(groupConfig).length > 0) {
                    config[group] = groupConfig;
                }
            }
            if (Object.keys(config).length > 0) {
                return config;
            }
            else {
                return null;
            }
        }
    }

    var ContributionSurveyRowSigningBehavior;
    (function (ContributionSurveyRowSigningBehavior) {
        ContributionSurveyRowSigningBehavior["Always"] = "always";
        ContributionSurveyRowSigningBehavior["AlwaysTrace"] = "alwaysTrace";
        ContributionSurveyRowSigningBehavior["AlwaysTraceLastOnly"] = "alwaysTraceLastOnly";
        ContributionSurveyRowSigningBehavior["LastOnly"] = "lastOnly";
        ContributionSurveyRowSigningBehavior["Never"] = "never";
    })(ContributionSurveyRowSigningBehavior || (ContributionSurveyRowSigningBehavior = {}));

    var DeputyPageToolbarState;
    (function (DeputyPageToolbarState) {
        DeputyPageToolbarState[DeputyPageToolbarState["Open"] = 0] = "Open";
        DeputyPageToolbarState[DeputyPageToolbarState["Collapsed"] = 1] = "Collapsed";
        DeputyPageToolbarState[DeputyPageToolbarState["Hidden"] = 2] = "Hidden";
    })(DeputyPageToolbarState || (DeputyPageToolbarState = {}));

    /**
     * A configuration. Defines settings and setting groups.
     */
    class UserConfiguration extends ConfigurationBase {
        /**
         * @return the configuration from the current wiki.
         */
        static load() {
            const config = new UserConfiguration();
            try {
                if (mw.user.options.get(UserConfiguration.optionKey)) {
                    const decodedOptions = JSON.parse(mw.user.options.get(UserConfiguration.optionKey));
                    config.deserialize(decodedOptions);
                }
            }
            catch (e) {
                error(e, mw.user.options.get(UserConfiguration.optionKey));
                mw.hook('deputy.i18nDone').add(function notifyConfigFailure() {
                    mw.notify(mw.msg('deputy.loadError.userConfig'), {
                        type: 'error'
                    });
                    mw.hook('deputy.i18nDone').remove(notifyConfigFailure);
                });
                config.save();
            }
            return config;
        }
        /**
         * Creates a new Configuration.
         *
         * @param serializedData
         */
        constructor(serializedData = {}) {
            var _a;
            super();
            this.core = {
                /**
                 * Numerical code that identifies this config version. Increments for every breaking
                 * configuration file change.
                 */
                configVersion: new Setting({
                    defaultValue: UserConfiguration.configVersion,
                    displayOptions: { hidden: true },
                    alwaysSave: true
                }),
                language: new Setting({
                    defaultValue: mw.config.get('wgUserLanguage'),
                    displayOptions: { type: 'select' }
                }),
                modules: new Setting({
                    defaultValue: ['cci', 'ante', 'ia'],
                    displayOptions: { type: 'checkboxes' },
                    allowedValues: ['cci', 'ante', 'ia']
                }),
                portletNames: new Setting(generateEnumConfigurationProperties(PortletNameView, PortletNameView.Full)),
                seenAnnouncements: new Setting({
                    defaultValue: [],
                    displayOptions: { hidden: true }
                }),
                dangerMode: new Setting({
                    defaultValue: false,
                    displayOptions: {
                        type: 'checkbox'
                    }
                })
            };
            this.cci = {
                enablePageToolbar: new Setting({
                    defaultValue: true,
                    displayOptions: {
                        type: 'checkbox'
                    }
                }),
                showCvLink: new Setting({
                    defaultValue: true,
                    displayOptions: {
                        type: 'checkbox'
                    }
                }),
                showUsername: new Setting({
                    defaultValue: false,
                    displayOptions: {
                        type: 'checkbox'
                    }
                }),
                autoCollapseRows: new Setting({
                    defaultValue: false,
                    displayOptions: {
                        type: 'checkbox'
                    }
                }),
                autoShowDiff: new Setting({
                    defaultValue: false,
                    displayOptions: {
                        type: 'checkbox'
                    }
                }),
                maxRevisionsToAutoShowDiff: new Setting({
                    defaultValue: 2,
                    displayOptions: {
                        type: 'number',
                        // Force any due to self-reference
                        disabled: (config) => !config.cci.autoShowDiff.get(),
                        extraOptions: {
                            min: 1
                        }
                    }
                }),
                maxSizeToAutoShowDiff: new Setting({
                    defaultValue: 500,
                    displayOptions: {
                        type: 'number',
                        // Force any due to self-reference
                        disabled: (config) => !config.cci.autoShowDiff.get(),
                        extraOptions: {
                            min: -1
                        }
                    }
                }),
                forceUtc: new Setting({
                    defaultValue: false,
                    displayOptions: {
                        type: 'checkbox'
                    }
                }),
                signingBehavior: new Setting(generateEnumConfigurationProperties(ContributionSurveyRowSigningBehavior, ContributionSurveyRowSigningBehavior.Always)),
                signSectionArchive: new Setting({
                    defaultValue: true,
                    displayOptions: {
                        type: 'checkbox'
                    }
                }),
                openOldOnContinue: new Setting({
                    defaultValue: false,
                    displayOptions: {
                        type: 'checkbox'
                    }
                }),
                toolbarInitialState: new Setting(Object.assign(Object.assign({}, generateEnumSerializers(DeputyPageToolbarState, DeputyPageToolbarState.Open)), { defaultValue: DeputyPageToolbarState.Open, displayOptions: { hidden: true } }))
            };
            this.ante = {
                enableAutoMerge: new Setting({
                    defaultValue: false,
                    displayOptions: {
                        type: 'checkbox',
                        disabled: 'unimplemented'
                    }
                }),
                onSubmit: new Setting(generateEnumConfigurationProperties(CompletionAction, CompletionAction.Reload))
            };
            this.ia = {
                responses: new Setting(Object.assign(Object.assign({}, Setting.basicSerializers), { defaultValue: null, displayOptions: {
                        disabled: 'unimplemented',
                        type: 'unimplemented'
                    } })),
                enablePageToolbar: new Setting({
                    defaultValue: true,
                    displayOptions: {
                        type: 'checkbox',
                        disabled: 'unimplemented'
                    }
                }),
                defaultEntirePage: new Setting({
                    defaultValue: true,
                    displayOptions: {
                        type: 'checkbox'
                    }
                }),
                defaultFromUrls: new Setting({
                    defaultValue: true,
                    displayOptions: {
                        type: 'checkbox'
                    }
                }),
                onHide: new Setting(generateEnumConfigurationProperties(TripleCompletionAction, TripleCompletionAction.Reload)),
                onSubmit: new Setting(generateEnumConfigurationProperties(TripleCompletionAction, TripleCompletionAction.Reload)),
                onBatchSubmit: new Setting(generateEnumConfigurationProperties(CompletionAction, CompletionAction.Reload))
            };
            this.type = 'user';
            this.all = { core: this.core, cci: this.cci, ante: this.ante, ia: this.ia };
            if (serializedData) {
                this.deserialize(serializedData);
            }
            if (mw.storage.get(`mw-${UserConfiguration.optionKey}-lastVersion`) !== version) ;
            mw.storage.set(`mw-${UserConfiguration.optionKey}-lastVersion`, version);
            if ((_a = window.deputy) === null || _a === void 0 ? void 0 : _a.comms) {
                window.deputy.comms.addEventListener('userConfigUpdate', (e) => {
                    // Update the configuration based on another tab's message.
                    this.deserialize(e.data.config);
                });
            }
        }
        /**
         * Saves the configuration.
         */
        save() {
            return __awaiter(this, void 0, void 0, function* () {
                yield MwApi.action.saveOption(UserConfiguration.optionKey, JSON.stringify(this.serialize()));
            });
        }
    }
    UserConfiguration.configVersion = 1;
    UserConfiguration.optionKey = 'userjs-deputy';

    var deputySettingsStyles = ".deputy-setting {margin-bottom: 1em;}.deputy-setting > .oo-ui-fieldLayout-align-top .oo-ui-fieldLayout-header .oo-ui-labelElement-label {font-weight: bold;}.dp-mb {margin-bottom: 1em;}.deputy-about {display: flex;}.deputy-about > :first-child {flex: 0;}.deputy-about > :first-child > img {height: 5em;width: auto;}.ltr .deputy-about > :first-child {margin-right: 1em;}.rtl .deputy-about > :first-child {margin-left: 1em;}.deputy-about > :nth-child(2) {flex: 1;}.deputy-about > :nth-child(2) > :first-child > * {display: inline;}.deputy-about > :nth-child(2) > :first-child > :first-child {font-weight: bold;font-size: 2em;}.deputy-about > :nth-child(2) > :first-child > :nth-child(2) {color: gray;vertical-align: bottom;margin-left: 0.4em;}.deputy-about > :nth-child(2) > :not(:first-child) {margin-top: 0.5em;}.ltr .deputy-about + div > :not(:last-child) {margin-right: 0.5em;}.rtl .deputy-about + div > :not(:last-child) {margin-left: 0.5em;}.ltr .deputy-about + div {text-align: right;}.rtl .deputy-about + div {text-align: left;}";

    /* eslint-disable mediawiki/msg-doc */
    let InternalConfigurationGroupTabPanel$1;
    /**
     * Initializes the process element.
     */
    function initConfigurationGroupTabPanel$1() {
        InternalConfigurationGroupTabPanel$1 = class ConfigurationGroupTabPanel extends OO.ui.TabPanelLayout {
            /**
             * @return The {@Link Setting}s for this group.
             */
            get settings() {
                return this.config.config.all[this.config.group];
            }
            /**
             * @param config Configuration to be passed to the element.
             */
            constructor(config) {
                super(`configurationGroupPage_${config.group}`);
                this.config = config;
                this.mode = config.config instanceof UserConfiguration ? 'user' : 'wiki';
                if (this.mode === 'wiki') {
                    this.$element.append(new OO.ui.MessageWidget({
                        classes: [
                            'deputy', 'dp-mb'
                        ],
                        type: 'warning',
                        label: mw.msg('deputy.settings.dialog.wikiConfigWarning')
                    }).$element);
                }
                for (const settingKey of Object.keys(this.settings)) {
                    const setting = this.settings[settingKey];
                    if (setting.isHidden(this.config.config)) {
                        continue;
                    }
                    switch (setting.displayOptions.type) {
                        case 'checkbox':
                            this.$element.append(this.newCheckboxField(settingKey, setting));
                            break;
                        case 'checkboxes':
                            this.$element.append(this.newCheckboxesField(settingKey, setting));
                            break;
                        case 'radio':
                            this.$element.append(this.newRadioField(settingKey, setting));
                            break;
                        case 'text':
                            this.$element.append(this.newStringField(settingKey, setting, setting.displayOptions.extraOptions));
                            break;
                        case 'number':
                            this.$element.append(this.newNumberField(settingKey, setting, setting.displayOptions.extraOptions));
                            break;
                        case 'page':
                            this.$element.append(this.newPageField(settingKey, setting, setting.displayOptions.extraOptions));
                            break;
                        case 'code':
                            this.$element.append(this.newCodeField(settingKey, setting, setting.displayOptions.extraOptions));
                            break;
                        default:
                            this.$element.append(this.newUnimplementedField(settingKey));
                            break;
                    }
                }
            }
            /**
             * Sets up the tab item
             */
            setupTabItem() {
                this.tabItem.setLabel(this.getMsg(this.config.group));
                return this;
            }
            /**
             * @return the i18n message for this setting tab.
             *
             * @param messageKey
             */
            getMsg(messageKey) {
                return mw.msg(`deputy.setting.${this.mode}.${messageKey}`);
            }
            /**
             * Gets the i18n message for a given setting.
             *
             * @param settingKey
             * @param key
             * @return A localized string
             */
            getSettingMsg(settingKey, key) {
                return this.getMsg(`${this.config.group}.${settingKey}.${key}`);
            }
            /**
             * @param settingKey
             * @param allowedValues
             * @return a tuple array of allowed values that can be used in OOUI `items` parameters.
             */
            getAllowedValuesArray(settingKey, allowedValues) {
                const items = [];
                if (Array.isArray(allowedValues)) {
                    for (const key of allowedValues) {
                        const message = mw.message(`deputy.setting.${this.mode}.${this.config.group}.${settingKey}.${key}`);
                        items.push([key, message.exists() ? message.text() : key]);
                    }
                }
                else {
                    for (const key of Object.keys(allowedValues)) {
                        const message = mw.message(`deputy.setting.${this.mode}.${this.config.group}.${settingKey}.${key}`);
                        items.push([key, message.exists() ? message.text() : key]);
                    }
                }
                return items;
            }
            /**
             * Creates an unimplemented setting notice.
             *
             * @param settingKey
             * @return An HTMLElement of the given setting's field.
             */
            newUnimplementedField(settingKey) {
                const desc = mw.message(`deputy.setting.${this.mode}.${this.config.group}.${settingKey}.description`);
                return h_1("div", { class: "deputy-setting" },
                    h_1("b", null, this.getSettingMsg(settingKey, 'name')),
                    desc.exists() ? h_1("p", { style: { fontSize: '0.925em', color: '#54595d' } }, desc.text()) : '',
                    h_1("p", null, mw.msg('deputy.settings.dialog.unimplemented')));
            }
            /**
             * Creates a checkbox field.
             *
             * @param settingKey
             * @param setting
             * @return An HTMLElement of the given setting's field.
             */
            newCheckboxField(settingKey, setting) {
                const isDisabled = setting.isDisabled(this.config.config);
                const desc = mw.message(`deputy.setting.${this.mode}.${this.config.group}.${settingKey}.description`);
                const field = new OO.ui.CheckboxInputWidget({
                    selected: setting.get(),
                    disabled: isDisabled !== undefined && isDisabled !== false
                });
                const layout = new OO.ui.FieldLayout(field, {
                    align: 'inline',
                    label: this.getSettingMsg(settingKey, 'name'),
                    help: typeof isDisabled === 'string' ?
                        this.getSettingMsg(settingKey, isDisabled) :
                        desc.exists() ? desc.text() : undefined,
                    helpInline: true
                });
                field.on('change', () => {
                    setting.set(field.isSelected());
                    this.emit('change');
                });
                // Attach disabled re-checker
                this.on('change', () => {
                    field.setDisabled(!!setting.isDisabled(this.config.config));
                });
                return h_1("div", { class: "deputy-setting" }, unwrapWidget(layout));
            }
            /**
             * Creates a new checkbox set field.
             *
             * @param settingKey
             * @param setting
             * @return An HTMLElement of the given setting's field.
             */
            newCheckboxesField(settingKey, setting) {
                const isDisabled = setting.isDisabled(this.config.config);
                const desc = mw.message(`deputy.setting.${this.mode}.${this.config.group}.${settingKey}.description`);
                const field = new OO.ui.CheckboxMultiselectInputWidget({
                    value: setting.get(),
                    disabled: isDisabled !== undefined && isDisabled !== false,
                    options: this.getAllowedValuesArray(settingKey, setting.allowedValues)
                        .map(([key, label]) => ({ data: key, label }))
                });
                const layout = new OO.ui.FieldLayout(field, {
                    align: 'top',
                    label: this.getSettingMsg(settingKey, 'name'),
                    help: typeof isDisabled === 'string' ?
                        this.getSettingMsg(settingKey, isDisabled) :
                        desc.exists() ? desc.text() : undefined,
                    helpInline: true
                });
                // TODO: @types/oojs-ui limitation
                field.on('change', (items) => {
                    const finalData = Array.isArray(setting.allowedValues) ?
                        items :
                        field.getValue().map((v) => setting.allowedValues[v]);
                    setting.set(finalData);
                    this.emit('change');
                });
                // Attach disabled re-checker
                this.on('change', () => {
                    field.setDisabled(!!setting.isDisabled(this.config.config));
                });
                return h_1("div", { class: "deputy-setting" }, unwrapWidget(layout));
            }
            /**
             * Creates a new radio set field.
             *
             * @param settingKey
             * @param setting
             * @return An HTMLElement of the given setting's field.
             */
            newRadioField(settingKey, setting) {
                var _a;
                const isDisabled = setting.isDisabled(this.config.config);
                const desc = mw.message(`deputy.setting.${this.mode}.${this.config.group}.${settingKey}.description`);
                const field = new OO.ui.RadioSelectWidget({
                    disabled: isDisabled !== undefined && isDisabled !== false &&
                        !((_a = setting.displayOptions.readOnly) !== null && _a !== void 0 ? _a : false),
                    items: this.getAllowedValuesArray(settingKey, setting.allowedValues)
                        .map(([key, label]) => new OO.ui.RadioOptionWidget({
                        data: key,
                        label: label,
                        selected: setting.get() === key
                    })),
                    multiselect: false
                });
                const layout = new OO.ui.FieldLayout(field, {
                    align: 'top',
                    label: this.getSettingMsg(settingKey, 'name'),
                    help: typeof isDisabled === 'string' ?
                        this.getSettingMsg(settingKey, isDisabled) :
                        desc.exists() ? desc.text() : undefined,
                    helpInline: true
                });
                // OOUIRadioInputWidget
                field.on('select', (items) => {
                    const finalData = Array.isArray(setting.allowedValues) ?
                        items.data :
                        setting.allowedValues[items.data];
                    setting.set(finalData);
                    this.emit('change');
                });
                // Attach disabled re-checker
                this.on('change', () => {
                    field.setDisabled(!!setting.isDisabled(this.config.config));
                });
                return h_1("div", { class: "deputy-setting" }, unwrapWidget(layout));
            }
            /**
             * Creates a new field that acts like a string field.
             *
             * @param FieldClass
             * @param settingKey
             * @param setting
             * @param extraFieldOptions
             * @return A Deputy setting field
             */
            newStringLikeField(FieldClass, settingKey, setting, extraFieldOptions = {}) {
                var _a, _b, _c;
                const isDisabled = setting.isDisabled(this.config.config);
                const desc = mw.message(`deputy.setting.${this.mode}.${this.config.group}.${settingKey}.description`);
                const field = new FieldClass(Object.assign({ readOnly: (_a = setting.displayOptions.readOnly) !== null && _a !== void 0 ? _a : false, value: (_c = (_b = setting.serialize) === null || _b === void 0 ? void 0 : _b.call(setting, setting.get())) !== null && _c !== void 0 ? _c : setting.get(), disabled: isDisabled !== undefined && isDisabled !== false }, extraFieldOptions));
                const layout = new OO.ui.FieldLayout(field, {
                    align: 'top',
                    label: this.getSettingMsg(settingKey, 'name'),
                    help: typeof isDisabled === 'string' ?
                        this.getSettingMsg(settingKey, isDisabled) :
                        desc.exists() ? desc.text() : undefined,
                    helpInline: true
                });
                if (FieldClass === OO.ui.NumberInputWidget) {
                    field.on('change', (value) => {
                        setting.set(+value);
                        this.emit('change');
                    });
                }
                else {
                    field.on('change', (value) => {
                        setting.set(value);
                        this.emit('change');
                    });
                }
                // Attach disabled re-checker
                this.on('change', () => {
                    field.setDisabled(setting.isDisabled(this.config.config));
                });
                return h_1("div", { class: "deputy-setting" }, unwrapWidget(layout));
            }
            /**
             * Creates a new string setting field.
             *
             * @param settingKey
             * @param setting
             * @param extraFieldOptions
             * @return An HTMLElement of the given setting's field.
             */
            newStringField(settingKey, setting, extraFieldOptions) {
                return this.newStringLikeField(OO.ui.TextInputWidget, settingKey, setting, extraFieldOptions);
            }
            /**
             * Creates a new number setting field.
             *
             * @param settingKey
             * @param setting
             * @param extraFieldOptions
             * @return An HTMLElement of the given setting's field.
             */
            newNumberField(settingKey, setting, extraFieldOptions) {
                return this.newStringLikeField(OO.ui.NumberInputWidget, settingKey, setting, extraFieldOptions);
            }
            /**
             * Creates a new page title setting field.
             *
             * @param settingKey
             * @param setting
             * @param extraFieldOptions
             * @return An HTMLElement of the given setting's field.
             */
            newPageField(settingKey, setting, extraFieldOptions) {
                return this.newStringLikeField(mw.widgets.TitleInputWidget, settingKey, setting, extraFieldOptions);
            }
            /**
             * Creates a new code setting field.
             *
             * @param settingKey
             * @param setting
             * @param extraFieldOptions
             * @return An HTMLElement of the given setting's field.
             */
            newCodeField(settingKey, setting, extraFieldOptions) {
                return this.newStringLikeField(OO.ui.MultilineTextInputWidget, settingKey, setting, extraFieldOptions);
            }
        };
    }
    /**
     * Creates a new ConfigurationGroupTabPanel.
     *
     * @param config Configuration to be passed to the element.
     * @return A ConfigurationGroupTabPanel object
     */
    function ConfigurationGroupTabPanel (config) {
        if (!InternalConfigurationGroupTabPanel$1) {
            initConfigurationGroupTabPanel$1();
        }
        return new InternalConfigurationGroupTabPanel$1(config);
    }

    var deputySettingsEnglish = {
    	"deputy.about.version": "v$1 ($2)",
    	"deputy.about": "About",
    	"deputy.about.homepage": "Homepage",
    	"deputy.about.openSource": "Source",
    	"deputy.about.contact": "Contact",
    	"deputy.about.credit": "Deputy was made with the help of the English Wikipedia Copyright Cleanup WikiProject and the Wikimedia Foundation.",
    	"deputy.about.license": "Deputy is licensed under the [$1 Apache License 2.0]. The source code for Deputy is available on [$2 GitHub], and is free for everyone to view and suggest changes.",
    	"deputy.about.thirdParty": "Deputy is bundled with third party libraries to make development easier. All libraries have been vetted for user security and license compatibility. For more information, see the [$1 Licensing] section on Deputy's README.",
    	"deputy.about.buildInfo": "Deputy v$1 ($2), committed $3.",
    	"deputy.about.footer": "Made with love, coffee, and the tears of copyright editors.",
    	"deputy.settings.portlet": "Deputy preferences",
    	"deputy.settings.portlet.tooltip": "Opens a dialog to modify Deputy preferences",
    	"deputy.settings.wikiEditIntro.title": "This is a Deputy configuration page",
    	"deputy.settings.wikiEditIntro.current": "Deputy's active configuration comes from this page. Changing this page will affect the settings of all Deputy users on this wiki. Edit responsibly, and avoid making significant changes without prior discussion.",
    	"deputy.settings.wikiEditIntro.other": "This is a valid Deputy configuration page, but the configuration is currently being loaded from [[$1]]. If this becomes the active configuration page, changing it will affect the settings of all Deputy users on this wiki. Edit responsibly, and avoid making significant changes without prior discussion.",
    	"deputy.settings.wikiEditIntro.edit.current": "Modify configuration",
    	"deputy.settings.wikiEditIntro.edit.other": "Modify this configuration",
    	"deputy.settings.wikiEditIntro.edit.otherCurrent": "Modify the active configuration",
    	"deputy.settings.wikiEditIntro.edit.protected": "This page's protection settings do not allow you to edit the page.",
    	"deputy.settings.wikiOutdated": "Outdated configuration",
    	"deputy.settings.wikiOutdated.help": "Deputy has detected a change in this wiki's configuration for all Deputy users. We've automatically downloaded the changes for you, but you have to reload to apply the changes.",
    	"deputy.settings.wikiOutdated.reload": "Reload",
    	"deputy.settings.dialog.title": "Deputy Preferences",
    	"deputy.settings.dialog.unimplemented": "A way to modify this setting has not yet been implemented. Check back later!",
    	"deputy.settings.saved": "Preferences saved. Please refresh the page to see changes.",
    	"deputy.settings.dialog.wikiConfigWarning": "You are currently editing a wiki-wide Deputy configuration page. Changes made to this page may affect the settings of all Deputy users on this wiki. Edit responsibly, and avoid making significant changes without prior discussion.",
    	"deputy.setting.user.core": "Deputy",
    	"deputy.setting.user.core.language.name": "Language",
    	"deputy.setting.user.core.language.description": "Deputy's interface language. English (US) is used by default, and is used as a fallback if no translations are available. If the content of the wiki you work on is in a different language from the interface language, Deputy will need to load additional data to ensure edit summaries, text, etc., saved on-wiki match the wiki's content language. For this reason, we suggest keeping the interface language the same as the wiki's content language.",
    	"deputy.setting.user.core.modules.name": "Modules",
    	"deputy.setting.user.core.modules.description": "Choose the enabled Deputy modules. By default, all modules are enabled.\nDisabling specific modules won't make Deputy load faster, but it can remove\nUI features added by Deputy which may act as clutter when unused.",
    	"deputy.setting.user.core.modules.cci": "Contributor Copyright Investigations",
    	"deputy.setting.user.core.modules.ante": "{{int:deputy.ante}}",
    	"deputy.setting.user.core.modules.ia": "{{int:deputy.ia}}",
    	"deputy.setting.user.core.portletNames.name": "Portlet names",
    	"deputy.setting.user.core.portletNames.description": "Choose which names appear in the Deputy portlet (toolbox) links.",
    	"deputy.setting.user.core.portletNames.full": "Full names (e.g. Attribution Notice Template Editor)",
    	"deputy.setting.user.core.portletNames.short": "Shortened names (e.g. Attrib. Template Editor)",
    	"deputy.setting.user.core.portletNames.acronym": "Acronyms (e.g. ANTE)",
    	"deputy.setting.user.core.dangerMode.name": "Danger mode",
    	"deputy.setting.user.core.dangerMode.description": "Live on the edge. This disables most confirmations and warnings given by Deputy, only leaving potentially catastrophic actions, such as page edits which break templates. It also adds extra buttons meant for rapid case processing. Intended for clerk use; use with extreme caution.",
    	"deputy.setting.user.cci": "CCI",
    	"deputy.setting.user.cci.enablePageToolbar.name": "Enable page toolbar",
    	"deputy.setting.user.cci.enablePageToolbar.description": "Enables the page toolbar, which is used to quickly show tools, analysis options, and related case information on a page that is the subject of a CCI investigation.",
    	"deputy.setting.user.cci.showCvLink.name": "Show \"cv\" (\"copyvios\") link for revisions",
    	"deputy.setting.user.cci.showCvLink.description": "Show a \"cv\" link next to \"cur\" and \"prev\" on revision rows. This link will only appear if this wiki is configured to use Earwig's Copyvio Detector.",
    	"deputy.setting.user.cci.showUsername.name": "Show username",
    	"deputy.setting.user.cci.showUsername.description": "Show the username of the user who made the edit on revision rows. This may be redundant for cases which only have one editor.",
    	"deputy.setting.user.cci.autoCollapseRows.name": "Automatically collapse rows",
    	"deputy.setting.user.cci.autoCollapseRows.description": "Automatically collapse rows when the page is loaded. This is useful for cases where each row has many revisions, but may be annoying for cases where each row has few revisions.",
    	"deputy.setting.user.cci.autoShowDiff.name": "Automatically show diffs",
    	"deputy.setting.user.cci.autoShowDiff.description": "Enabling automatic loading of diffs. Configurable with two additional options to avoid loading too much content.",
    	"deputy.setting.user.cci.maxRevisionsToAutoShowDiff.name": "Maximum revisions to automatically show diff",
    	"deputy.setting.user.cci.maxRevisionsToAutoShowDiff.description": "The maximum number of revisions for a given page to automatically show the diff for each revision in the main interface.",
    	"deputy.setting.user.cci.maxSizeToAutoShowDiff.name": "Maximum size to automatically show diff",
    	"deputy.setting.user.cci.maxSizeToAutoShowDiff.description": "The maximum size of a diff to be automatically shown, if the diff will be automatically shown (see \"Maximum revisions to automatically show diff\"). Prevents extremely large diffs from opening. Set to -1 to show regardless of size.",
    	"deputy.setting.user.cci.forceUtc.name": "Force UTC time",
    	"deputy.setting.user.cci.forceUtc.description": "Forces Deputy to use UTC time whenever displaying dates and times, irregardless of your system's timezone or your MediaWiki time settings.",
    	"deputy.setting.user.cci.signingBehavior.name": "Row signing behavior",
    	"deputy.setting.user.cci.signingBehavior.description": "Choose how Deputy should behave when signing rows. By default, all rows are always signed with your signature (~~~~). You may configure Deputy to only sign the last row or never sign. You can also configure Deputy to leave a hidden trace behind (<!-- User:Example|2016-05-28T14:32:12Z -->), which helps Deputy (for other users) determine who assessed a row.",
    	"deputy.setting.user.cci.signingBehavior.always": "Always sign rows",
    	"deputy.setting.user.cci.signingBehavior.alwaysTrace": "Always leave a trace",
    	"deputy.setting.user.cci.signingBehavior.alwaysTraceLastOnly": "Always leave a trace, but sign the last row modified",
    	"deputy.setting.user.cci.signingBehavior.lastOnly": "Only sign the last row modified (prevents assessor recognition)",
    	"deputy.setting.user.cci.signingBehavior.never": "Never sign rows (prevents assessor recognition)",
    	"deputy.setting.user.cci.signSectionArchive.name": "Sign by default when archiving CCI sections",
    	"deputy.setting.user.cci.signSectionArchive.description": "If enabled, Deputy will enable the \"include my signature\" checkbox by default when archiving a CCI section.",
    	"deputy.setting.user.cci.openOldOnContinue.name": "Open old versions on continue",
    	"deputy.setting.user.cci.openOldOnContinue.description": "If enabled, all previously-open sections of a given case page will also be opened alongside the section where the \"continue CCI session\" link was clicked.",
    	"deputy.setting.user.ante": "ANTE",
    	"deputy.setting.user.ante.enableAutoMerge.name": "Merge automatically on run",
    	"deputy.setting.user.ante.enableAutoMerge.description": "If enabled, templates that can be merged will automatically be merged when the dialog opens.",
    	"deputy.setting.user.ante.enableAutoMerge.unimplemented": "This feature has not yet been implemented.",
    	"deputy.setting.user.ante.onSubmit.name": "Action on submit",
    	"deputy.setting.user.ante.onSubmit.description": "Choose what to do after editing attribution notice templates.",
    	"deputy.setting.user.ante.onSubmit.nothing": "Do nothing",
    	"deputy.setting.user.ante.onSubmit.reload": "Reload the page",
    	"deputy.setting.user.ia": "IA",
    	"deputy.setting.user.ia.responses.name": "Custom responses",
    	"deputy.setting.user.ia.responses.description": "A custom set of responses, or overrides for existing responses. If an entry\nwith the same key on both the wiki-wide configuration and the user configuration\nexists, the user configuration will override the wiki-wide configuration. Wiki-wide configuration responses can also be disabled locally. If this setting is empty, no overrides are made.",
    	"deputy.setting.user.ia.enablePageToolbar.name": "Enable page toolbar",
    	"deputy.setting.user.ia.enablePageToolbar.description": "If enabled, the page toolbar will be shown when dealing with CP cases. The IA page toolbar works slightly differently from the CCI page toolbar. Namely, it shows a button for responding instead of a status dropdown.",
    	"deputy.setting.user.ia.enablePageToolbar.unimplemented": "This feature has not yet been implemented.",
    	"deputy.setting.user.ia.defaultEntirePage.name": "Hide entire page by default",
    	"deputy.setting.user.ia.defaultEntirePage.description": "If enabled, the Infringement Assistant reporting window will hide the entire page by default.",
    	"deputy.setting.user.ia.defaultFromUrls.name": "Use URLs by default",
    	"deputy.setting.user.ia.defaultFromUrls.description": "If enabled, the Infringement Assistant reporting window will use URL inputs by default.",
    	"deputy.setting.user.ia.onHide.name": "Action on hide",
    	"deputy.setting.user.ia.onHide.description": "Choose what to do after the \"Hide content only\" button is selected and the relevant content is hidden from the page.",
    	"deputy.setting.user.ia.onHide.nothing": "Do nothing",
    	"deputy.setting.user.ia.onHide.reload": "Reload the page",
    	"deputy.setting.user.ia.onHide.redirect": "Redirect to the noticeboard page",
    	"deputy.setting.user.ia.onSubmit.name": "Action on submit",
    	"deputy.setting.user.ia.onSubmit.description": "Choose what to do after the \"Submit\" button is selected, the relevant content is hidden from the page, and the page is reported.",
    	"deputy.setting.user.ia.onSubmit.nothing": "Do nothing",
    	"deputy.setting.user.ia.onSubmit.reload": "Reload the page",
    	"deputy.setting.user.ia.onSubmit.redirect": "Redirect to the noticeboard page",
    	"deputy.setting.user.ia.onBatchSubmit.name": "Action on batch listing submit",
    	"deputy.setting.user.ia.onBatchSubmit.description": "When reporting a batch of pages, choose what to do after the \"Report\" button is selected and the pages are reported.",
    	"deputy.setting.user.ia.onBatchSubmit.nothing": "Do nothing",
    	"deputy.setting.user.ia.onBatchSubmit.reload": "Reload the noticeboard page",
    	"deputy.setting.wiki.core": "Core",
    	"deputy.setting.wiki.core.lastEdited.name": "Configuration last edited",
    	"deputy.setting.wiki.core.lastEdited.description": "The last time that this configuration was edited, as a timestamp. This is a way to ensure that all users are on the correct wiki-wide configuration version before changes are made. Checks are performed on every page load with Deputy.",
    	"deputy.setting.wiki.core.dispatchRoot.name": "Deputy Dispatch root URL",
    	"deputy.setting.wiki.core.dispatchRoot.description": "The URL to a Deputy Dispatch instance that can handle this wiki. Deputy Dispatch is a webserver responsible for centralizing and optimizing data used by Deputy, and can be used to reduce load on wikis. More information can be found at https://github.com/ChlodAlejandro/deputy-dispatch.",
    	"deputy.setting.wiki.core.changeTag.name": "Change tag",
    	"deputy.setting.wiki.core.changeTag.description": "Tag to use for all Deputy edits.",
    	"deputy.setting.wiki.cci": "CCI",
    	"deputy.setting.wiki.cci.enabled.name": "Enable contributor copyright investigations assistant",
    	"deputy.setting.wiki.cci.enabled.description": "Enables the CCI workflow assistant. This allows Deputy to replace the contribution survey found on CCI case pages with a graphical interface which works with other tabs to make the CCI workflow easier.",
    	"deputy.setting.wiki.cci.rootPage.name": "Root page",
    	"deputy.setting.wiki.cci.rootPage.description": "The main page that holds all subpages containing valid contribution copyright investigation cases.",
    	"deputy.setting.wiki.cci.headingMatch.name": "Heading title regular expression",
    	"deputy.setting.wiki.cci.headingMatch.description": "A regular expression that will be used to detect valid contribution surveyor heading. Since its usage is rather technical, this value should be edited by someone with technical knowledge of regular expressions.",
    	"deputy.setting.wiki.cci.collapseTop.name": "Collapsible wikitext (top)",
    	"deputy.setting.wiki.cci.collapseTop.description": "Placed just below a section heading when closing a contributor survey section. Use \"$1\" to denote user comments and signature. On the English Wikipedia, this is {{Template:collapse top}}. Other wikis may have an equivalent template. This should go hand in hand with \"{{int:deputy.setting.wiki.cci.collapseBottom.name}}\", as they are used as a pair.",
    	"deputy.setting.wiki.cci.collapseBottom.name": "Collapsible wikitext (bottom)",
    	"deputy.setting.wiki.cci.collapseBottom.description": "Placed at the end of a section when closing a contributor survey section. On the English Wikipedia, this is {{Template:collapse bottom}}. Other wikis may have an equivalent template.",
    	"deputy.setting.wiki.cci.earwigRoot.name": "Earwig's Copyvio Detector root URL",
    	"deputy.setting.wiki.cci.earwigRoot.description": "The URL to an instance of Earwig's Copyvio Detector that can handle this wiki. The official copyvio detector (copyvios.toolforge.org) can only handle Wikimedia wikis — you may change this behavior by specifying a custom instance that can process this wiki here.",
    	"deputy.setting.wiki.cci.resortRows.name": "Resort rows",
    	"deputy.setting.wiki.cci.resortRows.description": "Resort rows when saving the page. This is useful for cases where rows are added out of order, or when rows are added in a different order than they should be displayed.",
    	"deputy.setting.wiki.ante": "ANTE",
    	"deputy.setting.wiki.ante.enabled.name": "Enable the Attribution Notice Template Editor",
    	"deputy.setting.wiki.ante.enabled.description": "Enables ANTE for all users. ANTE is currently the least-optimized module for localization, and may not work for all wikis.",
    	"deputy.setting.wiki.ia": "IA",
    	"deputy.setting.wiki.ia.enabled.name": "Enable the Infringement Assistant",
    	"deputy.setting.wiki.ia.enabled.description": "Enables IA for all users. IA allows users to easily and graphically report pages with suspected or complicated copyright infringements.",
    	"deputy.setting.wiki.ia.rootPage.name": "Root page",
    	"deputy.setting.wiki.ia.rootPage.description": "The root page for Infringement Assistant. This should be the copyright problems noticeboard for this specific wiki. IA will only show quick response links for the root page and its subpages.",
    	"deputy.setting.wiki.ia.subpageFormat.name": "Subpage format",
    	"deputy.setting.wiki.ia.subpageFormat.description": "The format to use for subpages of the root page. This is a moment.js format string.",
    	"deputy.setting.wiki.ia.preload.name": "Preload",
    	"deputy.setting.wiki.ia.preload.description": "Defines the page content to preload the page with if a given subpage does not exist yet. This should be an existing page on-wiki. Leave blank to avoid using a preload entirely.",
    	"deputy.setting.wiki.ia.allowPresumptive.name": "Allow presumptive deletions",
    	"deputy.setting.wiki.ia.allowPresumptive.description": "Allows users to file listings for presumptive deletions. Note that the CCI setting \"Root page\" must be set for this to work, even if the \"CCI\" module is disabled entirely.",
    	"deputy.setting.wiki.ia.listingWikitext.name": "Listing wikitext",
    	"deputy.setting.wiki.ia.listingWikitext.description": "Defines the wikitext that will be used when adding listings to a noticeboard page. You may use \"$1\" to denote the page being reported, and \"$2\" for user comments (which shouldn't contain the signature).",
    	"deputy.setting.wiki.ia.listingWikitextMatch.name": "Regular expression for listings",
    	"deputy.setting.wiki.ia.listingWikitextMatch.description": "A regular expression that will be used to parse and detect listings on a given noticeboard page. Since its usage is rather technical, this value should be edited by someone with technical knowledge of regular expressions. This regular expression must provide three captured groups: group \"$1\" will catch any bullet point, space, or prefix, \"$2\" will catch the page title ONLY if the given page matches \"{{int:deputy.setting.wiki.ia.listingWikitext.name}}\" or \"{{int:deputy.setting.wiki.ia.batchListingWikitext.name}}\", and \"$3\" will catch the page title ONLY IF the page wasn't caught in \"$2\" (such as in cases where there is only a bare link to the page).",
    	"deputy.setting.wiki.ia.batchListingWikitext.name": "Batch listing wikitext",
    	"deputy.setting.wiki.ia.batchListingWikitext.description": "Defines the wikitext that will be used when adding batch listings to a noticeboard page. You may use \"$1\" to denote the page being reported, and \"$2\" for the list of pages (as determined by \"{{int:deputy.setting.wiki.ia.batchListingPageWikitext.name}}\") and \"$3\" for user comments (which doesn't contain the signature).",
    	"deputy.setting.wiki.ia.batchListingPageWikitext.name": "Batch listing page wikitext",
    	"deputy.setting.wiki.ia.batchListingPageWikitext.description": "Wikitext to use for every row of text in \"{{int:deputy.setting.wiki.ia.batchListingWikitext.name}}\". No line breaks are automatically added; these must be added into this string.",
    	"deputy.setting.wiki.ia.hideTemplate.name": "Content hiding wikitext (top)",
    	"deputy.setting.wiki.ia.hideTemplate.description": "Wikitext to hide offending content with. On the English Wikipedia, this is a usage of {{Template:copyvio}}. Other wikis may have an equivalent template. This should go hand in hand with \"{{int:deputy.setting.wiki.ia.hideTemplateBottom.name}}\", as they are used as a pair.",
    	"deputy.setting.wiki.ia.hideTemplateBottom.name": "Content hiding wikitext (bottom)",
    	"deputy.setting.wiki.ia.hideTemplateBottom.description": "Placed at the end of hidden content to hide only part of a page. On the English Wikipedia, this is {{Template:copyvio/bottom}}. Other wikis may have an equivalent template.",
    	"deputy.setting.wiki.ia.entirePageAppendBottom.name": "Append content hiding wikitext (bottom) when hiding an entire page",
    	"deputy.setting.wiki.ia.entirePageAppendBottom.description": "If enabled, the content hiding wikitext (bottom) will be appended to the end of the page when hiding the entire page. This avoids the \"missing end tag\" lint error, if the template is properly formatted.",
    	"deputy.setting.wiki.ia.responses.name": "Responses",
    	"deputy.setting.wiki.ia.responses.description": "Quick responses for copyright problems listings. Used by clerks to resolve specific listings or provide more information about the progress of a given listing."
    };

    /**
     * Get the nodes from a JQuery object and wraps it in an element.
     *
     * @param element The element to add the children into
     * @param $j The JQuery object
     * @return The original element, now with children
     */
    function unwrapJQ(element = h_1("span", null), $j) {
        $j.each((i, e) => element.append(e));
        return element;
    }

    let InternalConfigurationGroupTabPanel;
    /**
     * Initializes the process element.
     */
    function initConfigurationGroupTabPanel() {
        var _a;
        InternalConfigurationGroupTabPanel = (_a = class ConfigurationGroupTabPanel extends OO.ui.TabPanelLayout {
                /**
                 * @return The {@Link Setting}s for this group.
                 */
                get settings() {
                    return this.config.config.all[this.config.group];
                }
                /**
                 */
                constructor() {
                    super('configurationGroupPage_About');
                    this.$element.append(h_1("div", null,
                        h_1("div", { class: "deputy-about" },
                            h_1("div", { style: "flex: 0" },
                                h_1("img", { src: ConfigurationGroupTabPanel.logoUrl, alt: "Deputy logo" })),
                            h_1("div", { style: "flex: 1" },
                                h_1("div", null,
                                    h_1("div", null, mw.msg('deputy.name')),
                                    h_1("div", null, mw.msg('deputy.about.version', version, gitAbbrevHash))),
                                h_1("div", null, mw.msg('deputy.description')))),
                        h_1("div", null,
                            h_1("a", { href: "https://w.wiki/7NWR", target: "_blank" }, unwrapWidget(new OO.ui.ButtonWidget({
                                label: mw.msg('deputy.about.homepage'),
                                flags: ['progressive']
                            }))),
                            h_1("a", { href: "https://github.com/ChlodAlejandro/deputy", target: "_blank" }, unwrapWidget(new OO.ui.ButtonWidget({
                                label: mw.msg('deputy.about.openSource'),
                                flags: ['progressive']
                            }))),
                            h_1("a", { href: "https://w.wiki/7NWS", target: "_blank" }, unwrapWidget(new OO.ui.ButtonWidget({
                                label: mw.msg('deputy.about.contact'),
                                flags: ['progressive']
                            })))),
                        unwrapJQ(h_1("p", null), mw.message('deputy.about.credit').parseDom()),
                        unwrapJQ(h_1("p", null), mw.message('deputy.about.license', 'https://www.apache.org/licenses/LICENSE-2.0', 'https://github.com/ChlodAlejandro/deputy').parseDom()),
                        unwrapJQ(h_1("p", null), mw.message('deputy.about.thirdParty', 'https://github.com/ChlodAlejandro/deputy#licensing').parseDom()),
                        unwrapJQ(h_1("p", { style: { fontSize: '0.9em', color: 'darkgray' } }), mw.message('deputy.about.buildInfo', gitVersion, gitBranch, new Date(gitDate).toLocaleString()).parseDom()),
                        unwrapJQ(h_1("p", { style: { fontSize: '0.9em', color: 'darkgray' } }), mw.message('deputy.about.footer').parseDom())));
                }
                /**
                 * Sets up the tab item
                 */
                setupTabItem() {
                    this.tabItem.setLabel(mw.msg('deputy.about'));
                    return this;
                }
            },
            _a.logoUrl = 'https://upload.wikimedia.org/wikipedia/commons/2/2b/Deputy_logo.svg',
            _a);
    }
    /**
     * Creates a new ConfigurationGroupTabPanel.
     *
     * @return A ConfigurationGroupTabPanel object
     */
    function ConfigurationAboutTabPanel () {
        if (!InternalConfigurationGroupTabPanel) {
            initConfigurationGroupTabPanel();
        }
        return new InternalConfigurationGroupTabPanel();
    }

    let InternalConfigurationDialog;
    /**
     * Initializes the process element.
     */
    function initConfigurationDialog() {
        var _a;
        InternalConfigurationDialog = (_a = class ConfigurationDialog extends OO.ui.ProcessDialog {
                /**
                 *
                 * @param data
                 */
                constructor(data) {
                    super();
                    this.config = data.config;
                }
                /**
                 * @return The body height of this dialog.
                 */
                getBodyHeight() {
                    return 900;
                }
                /**
                 * Initializes the dialog.
                 */
                initialize() {
                    super.initialize();
                    this.layout = new OO.ui.IndexLayout();
                    this.layout.addTabPanels(this.generateGroupLayouts());
                    if (this.config instanceof UserConfiguration) {
                        this.layout.addTabPanels([ConfigurationAboutTabPanel()]);
                    }
                    this.$body.append(this.layout.$element);
                    return this;
                }
                /**
                 * Generate TabPanelLayouts for each configuration group.
                 *
                 * @return An array of TabPanelLayouts
                 */
                generateGroupLayouts() {
                    return Object.keys(this.config.all).map((group) => ConfigurationGroupTabPanel({
                        config: this.config,
                        group
                    }));
                }
                /**
                 *
                 * @param action
                 * @return An OOUI Process.
                 */
                getActionProcess(action) {
                    const process = super.getActionProcess();
                    if (action === 'save') {
                        process.next(this.config.save());
                        process.next(() => {
                            var _a, _b;
                            mw.notify(mw.msg('deputy.settings.saved'), {
                                type: 'success'
                            });
                            if (this.config.type === 'user') {
                                // Override local Deputy option, just in case the user wishes to
                                // change the configuration again.
                                mw.user.options.set(UserConfiguration.optionKey, this.config.serialize());
                                if ((_a = window.deputy) === null || _a === void 0 ? void 0 : _a.comms) {
                                    window.deputy.comms.send({
                                        type: 'userConfigUpdate',
                                        config: this.config.serialize()
                                    });
                                }
                            }
                            else if (this.config.type === 'wiki') {
                                // We know it is a WikiConfiguration, the instanceof check here
                                // is just for type safety.
                                if ((_b = window.deputy) === null || _b === void 0 ? void 0 : _b.comms) {
                                    window.deputy.comms.send({
                                        type: 'wikiConfigUpdate',
                                        config: {
                                            title: this.config.sourcePage.getPrefixedText(),
                                            editable: this.config.editable,
                                            wt: this.config.serialize()
                                        }
                                    });
                                }
                                // Reload the page.
                                window.location.reload();
                            }
                        });
                    }
                    process.next(() => {
                        this.close();
                    });
                    return process;
                }
            },
            _a.static = Object.assign(Object.assign({}, OO.ui.ProcessDialog.static), { name: 'configurationDialog', title: mw.msg('deputy.settings.dialog.title'), size: 'large', actions: [
                    {
                        flags: ['safe', 'close'],
                        icon: 'close',
                        label: mw.msg('deputy.ante.close'),
                        title: mw.msg('deputy.ante.close'),
                        invisibleLabel: true,
                        action: 'close'
                    },
                    {
                        action: 'save',
                        label: mw.msg('deputy.save'),
                        flags: ['progressive', 'primary']
                    }
                ] }),
            _a);
    }
    /**
     * Creates a new ConfigurationDialog.
     *
     * @param data
     * @return A ConfigurationDialog object
     */
    function ConfigurationDialogBuilder(data) {
        if (!InternalConfigurationDialog) {
            initConfigurationDialog();
        }
        return new InternalConfigurationDialog(data);
    }
    let attached = false;
    /**
     * Spawns a new configuration dialog.
     *
     * @param config
     */
    function spawnConfigurationDialog(config) {
        mw.loader.using([
            'oojs-ui-core', 'oojs-ui-windows', 'oojs-ui-widgets', 'mediawiki.widgets'
        ], () => {
            const dialog = ConfigurationDialogBuilder({ config });
            openWindow(dialog);
        });
    }
    /**
     * Attaches the "Deputy preferences" portlet link in the toolbox. Ensures that it doesn't
     * get attached twice.
     */
    function attachConfigurationDialogPortletLink() {
        return __awaiter(this, void 0, void 0, function* () {
            if (document.querySelector('#p-deputy-config') || attached) {
                return;
            }
            attached = true;
            mw.util.addCSS(deputySettingsStyles);
            yield DeputyLanguage.load('settings', deputySettingsEnglish);
            mw.util.addPortletLink('p-tb', '#', mw.msg('deputy.settings.portlet'), 'deputy-config', mw.msg('deputy.settings.portlet.tooltip')).addEventListener('click', () => {
                // Load a fresh version of the configuration - this way we can make
                // modifications live to the configuration without actually affecting
                // tool usage.
                spawnConfigurationDialog(UserConfiguration.load());
            });
        });
    }

    /**
     * @param config The current configuration (actively loaded, not the one being viewed)
     * @return An HTML element consisting of an OOUI MessageWidget
     */
    function WikiConfigurationEditIntro(config) {
        const current = config.onConfigurationPage();
        let buttons;
        if (current) {
            const editCurrent = new OO.ui.ButtonWidget({
                flags: ['progressive', 'primary'],
                label: mw.msg('deputy.settings.wikiEditIntro.edit.current'),
                disabled: !mw.config.get('wgIsProbablyEditable'),
                title: mw.config.get('wgIsProbablyEditable') ?
                    undefined : mw.msg('deputy.settings.wikiEditIntro.edit.protected')
            });
            editCurrent.on('click', () => {
                spawnConfigurationDialog(config);
            });
            buttons = [editCurrent];
        }
        else {
            const editCurrent = new OO.ui.ButtonWidget({
                flags: ['progressive', 'primary'],
                label: mw.msg('deputy.settings.wikiEditIntro.edit.otherCurrent'),
                disabled: !config.editable,
                title: config.editable ?
                    undefined : mw.msg('deputy.settings.wikiEditIntro.edit.protected')
            });
            editCurrent.on('click', () => __awaiter(this, void 0, void 0, function* () {
                spawnConfigurationDialog(config);
            }));
            const editOther = new OO.ui.ButtonWidget({
                flags: ['progressive'],
                label: mw.msg('deputy.settings.wikiEditIntro.edit.other'),
                disabled: !mw.config.get('wgIsProbablyEditable'),
                title: mw.config.get('wgIsProbablyEditable') ?
                    undefined : mw.msg('deputy.settings.wikiEditIntro.edit.protected')
            });
            editOther.on('click', () => __awaiter(this, void 0, void 0, function* () {
                spawnConfigurationDialog(yield config.static.load(normalizeTitle()));
            }));
            buttons = [editCurrent, editOther];
        }
        const messageBox = DeputyMessageWidget({
            classes: [
                'deputy', 'dp-mb'
            ],
            type: 'notice',
            title: mw.msg('deputy.settings.wikiEditIntro.title'),
            message: current ?
                mw.msg('deputy.settings.wikiEditIntro.current') :
                unwrapJQ(h_1("span", null), mw.message('deputy.settings.wikiEditIntro.other', config.sourcePage.getPrefixedText()).parseDom()),
            actions: buttons
        });
        const box = unwrapWidget(messageBox);
        box.classList.add('deputy', 'deputy-wikiConfig-intro');
        return box;
    }

    /* eslint-disable max-len */
    /*
     * Replacement polyfills for wikis that have no configured templates.
     * Used in WikiConfiguration, to permit a seamless OOB experience.
     */
    /** `{{collapse top}}` equivalent */
    const collapseTop = `
{| class="mw-collapsible mw-collapsed" style="border:1px solid #C0C0C0;width:100%"
! <div style="background:#CCFFCC;">$1</div>
|-
|
`.trimStart();
    /** `{{collapse bottom}}` equivalent */
    const collapseBottom = `
|}`;
    /** `* {{subst:article-cv|1=$1}} $2 ~~~~` equivalent */
    const listingWikitext = '* [[$1]] $2 ~~~~';
    /**
     * Polyfill for the following:
     * `; {{anchor|1={{delink|$1}}}} $1
     * $2
     * $3 ~~~~`
     */
    const batchListingWikitext = `*; <span style="display: none;" id="$1"></span> $1
$2
$3`;
    /**
     * Inserted and chained as part of $2 in `batchListingWikitext`.
     * Equivalent of `* {{subst:article-cv|1=$1}}\n`. Newline is intentional.
     */
    const batchListingPageWikitext = '* [[$1]]\n';
    /**
     * `{{subst:copyvio|url=$1|fullpage=$2}}` equivalent
     */
    const copyvioTop = `<div style="padding: 8px; border: 4px solid #0298b1;">
<div style="font-size: 1.2rem"><b>{{int:deputy.ia.content.copyvio}}</b></div>
<div>{{int:deputy.ia.content.copyvio.help}}</div>
{{if:$1|<div>{{if:$presumptive|{{int:deputy.ia.content.copyvio.from.pd}} $1|{{int:deputy.ia.content.copyvio.from}} $1}}</div>}}
</div>
<!-- {{int:deputy.ia.content.copyvio.content}} -->
<div class="copyvio" style="display: none">`;
    /**
     * `{{subst:copyvio/bottom}}` equivalent.
     */
    const copyvioBottom = `
</div>`;

    /**
     * @return A MessageWidget for reloading a page with an outdated configuration.
     */
    function ConfigurationReloadBanner() {
        const reloadButton = new OO.ui.ButtonWidget({
            flags: ['progressive', 'primary'],
            label: mw.msg('deputy.settings.wikiOutdated.reload')
        });
        const messageBox = DeputyMessageWidget({
            classes: [
                'deputy', 'dp-mb', 'dp-wikiConfigUpdateMessage'
            ],
            type: 'notice',
            title: mw.msg('deputy.settings.wikiOutdated'),
            message: mw.msg('deputy.settings.wikiOutdated.help'),
            actions: [reloadButton]
        });
        reloadButton.on('click', () => __awaiter(this, void 0, void 0, function* () {
            window.location.reload();
        }));
        const box = unwrapWidget(messageBox);
        box.style.fontSize = 'calc(1em * 0.875)';
        return box;
    }

    var WikiConfigurationLocations = [
        'MediaWiki:Deputy-config.json',
        // Prioritize interface protected page over Project namespace
        'User:Chlod/Scripts/Deputy/configuration.json',
        'Project:Deputy/configuration.json'
    ];

    /**
     * Log to the console.
     *
     * @param {...any} data
     */
    function log(...data) {
        console.log('[Deputy]', ...data);
    }

    /**
     * Wiki-wide configuration. This is applied to all users of the wiki, and has
     * the potential to break things for EVERYONE if not set to proper values.
     *
     * As much as possible, the correct configuration location should be protected
     * to avoid vandalism or bad-faith changes.
     *
     * This configuration works if specific settings are set. In other words, some
     * features of Deputy are disabled unless Deputy has been configured. This is
     * to avoid messing with existing on-wiki processes.
     */
    class WikiConfiguration extends ConfigurationBase {
        /**
         * Loads the configuration from a set of possible sources.
         *
         * @param sourcePage The specific page to load from
         * @return A WikiConfiguration object
         */
        static load(sourcePage) {
            return __awaiter(this, void 0, void 0, function* () {
                if (sourcePage) {
                    // Explicit source given. Do not load from local cache.
                    return this.loadFromWiki(sourcePage);
                }
                else {
                    return this.loadFromLocal();
                }
            });
        }
        /**
         * Loads the wiki configuration from localStorage and/or MediaWiki
         * settings. This allows for faster loads at the expense of a (small)
         * chance of outdated configuration.
         *
         * The localStorage layer allows fast browser-based caching. If a user
         * is logging in again on another device, the user configuration
         * will automatically be sent to the client, lessening turnaround time.
         * If all else fails, the configuration will be loaded from the wiki.
         *
         * @return A WikiConfiguration object.
         */
        static loadFromLocal() {
            return __awaiter(this, void 0, void 0, function* () {
                let configInfo;
                // If `mw.storage.get` returns `false` or `null`, it'll be thrown up.
                let rawConfigInfo = mw.storage.get(WikiConfiguration.optionKey);
                // Try to grab it from user options, if it exists.
                if (!rawConfigInfo) {
                    rawConfigInfo = mw.user.options.get(WikiConfiguration.optionKey);
                }
                if (typeof rawConfigInfo === 'string') {
                    try {
                        configInfo = JSON.parse(rawConfigInfo);
                    }
                    catch (e) {
                        // Bad local! Switch to non-local.
                        error('Failed to get Deputy wiki configuration', e);
                        return this.loadFromWiki();
                    }
                }
                else {
                    log('No locally-cached Deputy configuration, pulling from wiki.');
                    return this.loadFromWiki();
                }
                if (configInfo) {
                    return new WikiConfiguration(new mw.Title(configInfo.title.title, configInfo.title.namespace), JSON.parse(configInfo.wt), configInfo.editable);
                }
                else {
                    return this.loadFromWiki();
                }
            });
        }
        /**
         * Loads the configuration from the current wiki.
         *
         * @param sourcePage The specific page to load from
         * @return A WikiConfiguration object
         */
        static loadFromWiki(sourcePage) {
            return __awaiter(this, void 0, void 0, function* () {
                const configPage = sourcePage ? Object.assign({ title: sourcePage }, yield (() => __awaiter(this, void 0, void 0, function* () {
                    const content = yield getPageContent(sourcePage, {
                        prop: 'revisions|info',
                        intestactions: 'edit',
                        fallbacktext: '{}'
                    });
                    return {
                        wt: content,
                        editable: content.page.actions.edit
                    };
                }))()) : yield this.loadConfigurationWikitext();
                try {
                    // Attempt save of configuration to local options (if not explicitly loaded)
                    if (sourcePage == null) {
                        mw.storage.set(WikiConfiguration.optionKey, JSON.stringify(configPage));
                    }
                    return new WikiConfiguration(configPage.title, JSON.parse(configPage.wt), configPage.editable);
                }
                catch (e) {
                    error(e, configPage);
                    mw.hook('deputy.i18nDone').add(function notifyConfigFailure() {
                        mw.notify(mw.msg('deputy.loadError.wikiConfig'), {
                            type: 'error'
                        });
                        mw.hook('deputy.i18nDone').remove(notifyConfigFailure);
                    });
                    return null;
                }
            });
        }
        /**
         * Loads the wiki-wide configuration from a set of predefined locations.
         * See {@link WikiConfiguration#configLocations} for a full list.
         *
         * @return The string text of the raw configuration, or `null` if a configuration was not found.
         */
        static loadConfigurationWikitext() {
            return __awaiter(this, void 0, void 0, function* () {
                const response = yield MwApi.action.get({
                    action: 'query',
                    prop: 'revisions|info',
                    rvprop: 'content',
                    rvslots: 'main',
                    rvlimit: 1,
                    intestactions: 'edit',
                    redirects: true,
                    titles: WikiConfiguration.configLocations.join('|')
                });
                const redirects = toRedirectsObject(response.query.redirects, response.query.normalized);
                for (const page of WikiConfiguration.configLocations) {
                    const title = normalizeTitle(redirects[page] || page).getPrefixedText();
                    const pageInfo = response.query.pages.find((p) => p.title === title);
                    if (!pageInfo.missing) {
                        return {
                            title: normalizeTitle(pageInfo.title),
                            wt: pageInfo.revisions[0].slots.main.content,
                            editable: pageInfo.actions.edit
                        };
                    }
                }
                return null;
            });
        }
        /**
         * Check if the current page being viewed is a valid configuration page.
         *
         * @param page
         * @return `true` if the current page is a valid configuration page.
         */
        static isConfigurationPage(page) {
            if (page == null) {
                page = new mw.Title(mw.config.get('wgPageName'));
            }
            return this.configLocations.some((v) => equalTitle(page, normalizeTitle(v)));
        }
        /**
         * @param sourcePage
         * @param serializedData
         * @param editable Whether the configuration is editable by the current user or not.
         */
        constructor(sourcePage, serializedData, editable) {
            var _a;
            super();
            this.sourcePage = sourcePage;
            this.serializedData = serializedData;
            this.editable = editable;
            // Used to avoid circular dependencies.
            this.static = WikiConfiguration;
            this.core = {
                /**
                 * Numerical code that identifies this config version. Increments for every breaking
                 * configuration file change.
                 */
                configVersion: new Setting({
                    defaultValue: WikiConfiguration.configVersion,
                    displayOptions: { hidden: true },
                    alwaysSave: true
                }),
                lastEdited: new Setting({
                    defaultValue: 0,
                    displayOptions: { hidden: true },
                    alwaysSave: true
                }),
                dispatchRoot: new Setting({
                    serialize: (v) => v.href,
                    deserialize: (v) => new URL(v),
                    defaultValue: new URL('https://deputy.toolforge.org/'),
                    displayOptions: { type: 'text' },
                    alwaysSave: true
                }),
                changeTag: new Setting({
                    defaultValue: null,
                    displayOptions: { type: 'text' }
                })
            };
            this.cci = {
                enabled: new Setting({
                    defaultValue: false,
                    displayOptions: { type: 'checkbox' }
                }),
                rootPage: new Setting({
                    serialize: (v) => v === null || v === void 0 ? void 0 : v.getPrefixedText(),
                    deserialize: (v) => new mw.Title(v),
                    defaultValue: null,
                    displayOptions: { type: 'page' }
                }),
                headingMatch: new Setting({
                    defaultValue: '(Page|Article|Local file|File)s? \\d+ (to|through) \\d+',
                    displayOptions: { type: 'text' }
                }),
                collapseTop: new Setting({
                    defaultValue: collapseTop,
                    displayOptions: { type: 'code' }
                }),
                collapseBottom: new Setting({
                    defaultValue: collapseBottom,
                    displayOptions: { type: 'code' }
                }),
                earwigRoot: new Setting({
                    serialize: (v) => v.href,
                    deserialize: (v) => new URL(v),
                    defaultValue: new URL('https://copyvios.toolforge.org/'),
                    displayOptions: { type: 'text' },
                    alwaysSave: true
                }),
                resortRows: new Setting({
                    defaultValue: true,
                    displayOptions: { type: 'checkbox' }
                })
            };
            this.ante = {
                enabled: new Setting({
                    defaultValue: false,
                    displayOptions: { type: 'checkbox' }
                })
            };
            this.ia = {
                enabled: new Setting({
                    defaultValue: false,
                    displayOptions: { type: 'checkbox' }
                }),
                rootPage: new Setting({
                    serialize: (v) => v === null || v === void 0 ? void 0 : v.getPrefixedText(),
                    deserialize: (v) => new mw.Title(v),
                    defaultValue: null,
                    displayOptions: { type: 'page' }
                }),
                subpageFormat: new Setting({
                    defaultValue: 'YYYY MMMM D',
                    displayOptions: { type: 'text' }
                }),
                preload: new Setting({
                    serialize: (v) => { var _a, _b; return ((_b = (_a = v === null || v === void 0 ? void 0 : v.trim()) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) === 0 ? null : v.trim(); },
                    defaultValue: null,
                    displayOptions: { type: 'page' }
                }),
                allowPresumptive: new Setting({
                    defaultValue: true,
                    displayOptions: { type: 'checkbox' }
                }),
                listingWikitext: new Setting({
                    defaultValue: listingWikitext,
                    displayOptions: { type: 'code' }
                }),
                /**
                 * $1 - Title of the batch
                 * $2 - List of pages (newlines should be added in batchListingPageWikitext).
                 * $3 - User comment
                 */
                batchListingWikitext: new Setting({
                    defaultValue: batchListingWikitext,
                    displayOptions: { type: 'code' }
                }),
                /**
                 * $1 - Page to include
                 */
                batchListingPageWikitext: new Setting({
                    defaultValue: batchListingPageWikitext,
                    displayOptions: { type: 'code' }
                }),
                /**
                 * @see {@link CopyrightProblemsListing#articleCvRegex}
                 *
                 * This should match both normal and batch listings.
                 */
                listingWikitextMatch: new Setting({
                    defaultValue: '(\\*\\s*)?\\[\\[([^\\]]+)\\]\\]',
                    displayOptions: { type: 'code' }
                }),
                hideTemplate: new Setting({
                    defaultValue: copyvioTop,
                    displayOptions: { type: 'code' }
                }),
                hideTemplateBottom: new Setting({
                    defaultValue: copyvioBottom,
                    displayOptions: { type: 'code' }
                }),
                entirePageAppendBottom: new Setting({
                    defaultValue: true,
                    displayOptions: { type: 'checkbox' }
                }),
                responses: new Setting(Object.assign(Object.assign({}, Setting.basicSerializers), { defaultValue: null, displayOptions: { type: 'unimplemented' } }))
            };
            this.type = 'wiki';
            this.all = { core: this.core, cci: this.cci, ante: this.ante, ia: this.ia };
            /**
             * Set to true when this configuration is outdated based on latest data. Usually adds banners
             * to UI interfaces saying a new version of the configuration is available, and that it should
             * be used whenever possible.
             *
             * TODO: This doesn't do what the documentations says yet.
             */
            this.outdated = false;
            if (serializedData) {
                this.deserialize(serializedData);
            }
            if ((_a = window.deputy) === null || _a === void 0 ? void 0 : _a.comms) {
                // Communications is available. Register a listener.
                window.deputy.comms.addEventListener('wikiConfigUpdate', (e) => {
                    this.update(Object.assign({}, e.data.config, {
                        title: normalizeTitle(e.data.config.title)
                    }));
                });
            }
        }
        /**
         * Check for local updates, and update the local configuration as needed.
         *
         * @param sourceConfig A serialized version of the configuration based on a wiki
         * page configuration load.
         */
        update(sourceConfig) {
            return __awaiter(this, void 0, void 0, function* () {
                // Asynchronously load from the wiki.
                let fromWiki;
                if (sourceConfig) {
                    fromWiki = sourceConfig;
                }
                else {
                    // Asynchronously load from the wiki.
                    fromWiki = yield WikiConfiguration.loadConfigurationWikitext();
                    if (fromWiki == null) {
                        // No configuration found on the wiki.
                        return;
                    }
                }
                const liveWikiConfig = JSON.parse(fromWiki.wt);
                // Attempt save if on-wiki config found and doesn't match local.
                // Doesn't need to be from the same config page, since this usually means a new config
                // page was made, and we need to switch to it.
                if (this.core.lastEdited.get() < liveWikiConfig.core.lastEdited) {
                    if (liveWikiConfig.core.configVersion > WikiConfiguration.configVersion) {
                        // Don't update if the config version is higher than ours. We don't want
                        // to load in the config of a newer version, as it may break things.
                        // Deputy should load in the newer version of the script soon enough,
                        // and the config will be parsed by a version that supports it.
                        warn(`Expected wiki configuration version ${this.core.configVersion.get()}, but found ${liveWikiConfig.core.configVersion}. New configuration will not be loaded.`);
                        return;
                    }
                    else if (liveWikiConfig.core.configVersion < WikiConfiguration.configVersion) {
                        // Version change detected.
                        // Do nothing... for now.
                        // HINT: Update configuration
                        warn(`Expected wiki configuration version ${this.core.configVersion.get()}, but found ${liveWikiConfig.core.configVersion}. Proceeding anyway...`);
                    }
                    const onSuccess = () => {
                        var _a;
                        // Only mark outdated after saving, so we don't indirectly cause a save operation
                        // to cancel.
                        this.outdated = true;
                        // Attempt to add site notice.
                        if (document.querySelector('.dp-wikiConfigUpdateMessage') == null) {
                            (_a = document.getElementById('siteNotice')) === null || _a === void 0 ? void 0 : _a.insertAdjacentElement('afterend', ConfigurationReloadBanner());
                        }
                    };
                    // If updated from a source config (other Deputy tab), do not attempt to save
                    // to MediaWiki settings. This is most likely already saved by the original tab
                    // that sent the comms message.
                    if (!sourceConfig) {
                        // Use `liveWikiConfig`, since this contains the compressed version and is more
                        // bandwidth-friendly.
                        const rawConfigInfo = JSON.stringify({
                            title: fromWiki.title,
                            editable: fromWiki.editable,
                            wt: JSON.stringify(liveWikiConfig)
                        });
                        // Save to local storage.
                        mw.storage.set(WikiConfiguration.optionKey, rawConfigInfo);
                        // Save to user options (for faster first-load times).
                        yield MwApi.action.saveOption(WikiConfiguration.optionKey, rawConfigInfo).then(() => {
                            var _a;
                            if ((_a = window.deputy) === null || _a === void 0 ? void 0 : _a.comms) {
                                // Broadcast the update to other tabs.
                                window.deputy.comms.send({
                                    type: 'wikiConfigUpdate',
                                    config: {
                                        title: fromWiki.title.getPrefixedText(),
                                        editable: fromWiki.editable,
                                        wt: liveWikiConfig
                                    }
                                });
                            }
                            onSuccess();
                        }).catch(() => {
                            // silently fail
                        });
                    }
                    else {
                        onSuccess();
                    }
                }
            });
        }
        /**
         * Saves the configuration on-wiki. Does not automatically generate overrides.
         */
        save() {
            return __awaiter(this, void 0, void 0, function* () {
                // Update last edited number
                this.core.lastEdited.set(Date.now());
                yield MwApi.action.postWithEditToken(Object.assign(Object.assign({}, changeTag(yield window.deputy.getWikiConfig())), { action: 'edit', title: this.sourcePage.getPrefixedText(), text: JSON.stringify(this.serialize()) }));
            });
        }
        /**
         * Check if the current page being viewed is the active configuration page.
         *
         * @param page
         * @return `true` if the current page is the active configuration page.
         */
        onConfigurationPage(page) {
            return equalTitle(page !== null && page !== void 0 ? page : mw.config.get('wgPageName'), this.sourcePage);
        }
        /**
         * Actually displays the banner which allows an editor to modify the configuration.
         */
        displayEditBanner() {
            return __awaiter(this, void 0, void 0, function* () {
                mw.loader.using(['oojs', 'oojs-ui'], () => {
                    if (document.getElementsByClassName('deputy-wikiConfig-intro').length > 0) {
                        return;
                    }
                    document.getElementById('mw-content-text').insertAdjacentElement('afterbegin', WikiConfigurationEditIntro(this));
                });
            });
        }
        /**
         * Shows the configuration edit intro banner, if applicable on this page.
         *
         * @return void
         */
        prepareEditBanners() {
            return __awaiter(this, void 0, void 0, function* () {
                if (['view', 'diff'].indexOf(mw.config.get('wgAction')) === -1) {
                    return;
                }
                if (document.getElementsByClassName('deputy-wikiConfig-intro').length > 0) {
                    return;
                }
                if (this.onConfigurationPage()) {
                    return this.displayEditBanner();
                }
                else if (WikiConfiguration.isConfigurationPage()) {
                    return this.displayEditBanner();
                }
            });
        }
    }
    WikiConfiguration.configVersion = 2;
    WikiConfiguration.optionKey = 'userjs-deputy-wiki';
    WikiConfiguration.configLocations = WikiConfigurationLocations;

    /**
     * A Deputy module. Modules are parts of Deputy that can usually be removed
     * and turned into standalone components that can load without Deputy.
     */
    class DeputyModule {
        /**
         * @return The responsible window manager for this class.
         */
        get windowManager() {
            if (!this.deputy) {
                if (!this._windowManager) {
                    this._windowManager = new OO.ui.WindowManager();
                    document.body.appendChild(unwrapWidget(this._windowManager));
                }
                return this._windowManager;
            }
            else {
                return this.deputy.windowManager;
            }
        }
        /**
         * @return the configuration handler for this module. If Deputy is loaded, this reuses
         * the configuration handler of Deputy.
         */
        get config() {
            var _a;
            if (!this.deputy) {
                return (_a = this._config) !== null && _a !== void 0 ? _a : (this._config = UserConfiguration.load());
            }
            else {
                return this.deputy.config;
            }
        }
        /**
         * @return the wiki-wide configuration handler for this module. If Deputy is loaded,
         * this reuses the configuration handler of Deputy. Since the wiki config is loaded
         * asynchronously, this may not be populated at runtime. Only use it if you're sure
         * that `preInit` has already been called and finished.
         */
        get wikiConfig() {
            return this.deputy ? this.deputy.wikiConfig : this._wikiConfig;
        }
        /**
         *
         * @param deputy
         */
        constructor(deputy) {
            this.deputy = deputy;
        }
        /**
         * Get the module key for this module. Allows modules to be identified with a different
         * configuration key.
         *
         * @return The module key. the module name by default.
         */
        getModuleKey() {
            return this.getName();
        }
        /**
         * Load the language pack for this module, with a fallback in case one could not be
         * loaded.
         *
         * @param fallback The fallback to use if a language pack could not be loaded.
         */
        loadLanguages(fallback) {
            return __awaiter(this, void 0, void 0, function* () {
                yield Promise.all([
                    DeputyLanguage.load(this.getName(), fallback),
                    DeputyLanguage.load('shared', deputySharedEnglish),
                    DeputyLanguage.loadMomentLocale()
                ]);
            });
        }
        /**
         * Pre-initialize the module. This is the opportunity of the module to load language
         * strings, append important UI elements, add portlets, etc.
         *
         * @param languageFallback The fallback language pack to use if one could not be loaded.
         */
        preInit(languageFallback) {
            var _a;
            return __awaiter(this, void 0, void 0, function* () {
                yield this.getWikiConfig();
                if (((_a = this.wikiConfig[this.getModuleKey()]) === null || _a === void 0 ? void 0 : _a.enabled.get()) !== true) {
                    // Stop loading here.
                    warn(`Preinit for ${this.getName()} cancelled; module is disabled.`);
                    return false;
                }
                yield this.loadLanguages(languageFallback);
                yield attachConfigurationDialogPortletLink();
                yield this.wikiConfig.prepareEditBanners();
                return true;
            });
        }
        /**
         * Gets the wiki-specific configuration for Deputy.
         *
         * @return A promise resolving to the loaded configuration
         */
        getWikiConfig() {
            var _a;
            return __awaiter(this, void 0, void 0, function* () {
                if (this.deputy) {
                    return this.deputy.getWikiConfig();
                }
                else {
                    return (_a = this._wikiConfig) !== null && _a !== void 0 ? _a : (this._wikiConfig = yield WikiConfiguration.load());
                }
            });
        }
    }

    var cteStyles = ".copied-template-editor .oo-ui-window-frame {width: 1000px !important;}.copied-template-editor .oo-ui-menuLayout > .oo-ui-menuLayout-menu {height: 20em;width: 20em;}.copied-template-editor .oo-ui-menuLayout > .oo-ui-menuLayout-content {left: 20em;}.cte-preview .copiednotice {margin-left: 0;margin-right: 0;}.cte-merge-panel {padding: 16px;z-index: 20;border: 1px solid lightgray;margin-bottom: 8px;}.copied-template-editor .oo-ui-bookletLayout-outlinePanel {bottom: 32px;}.cte-actionPanel {height: 32px;width: 100%;position: absolute;bottom: 0;z-index: 1;background-color: white;border-top: 1px solid #c8ccd1;}.cte-actionPanel > .oo-ui-buttonElement {display: inline-block;margin: 0 !important;}.cte-templateOptions {margin: 8px;display: flex;}.cte-templateOptions > * {flex: 1;}.cte-fieldset {border: 1px solid gray;background-color: #ddf7ff;padding: 16px;min-width: 200px;clear: both;}.cte-fieldset-date {float: left;margin-top: 10px !important;}.cte-fieldset-advswitch {float: right;}.cte-fieldset-advswitch .oo-ui-fieldLayout-field,.cte-fieldset-date .oo-ui-fieldLayout-field {display: inline-block !important;}.cte-fieldset-advswitch .oo-ui-fieldLayout-header {display: inline-block !important;margin-right: 16px;}.cte-fieldset-date .oo-ui-fieldLayout-field {min-width: 18em;}.cte-fieldset .mw-widget-dateInputWidget {max-width: unset;}.cte-page-row:not(:last-child),.cte-page-template:not(:last-child) {padding-bottom: 0 !important;}.cte-page-template + .cte-page-row {padding-top: 0 !important;}.copied-template-editor .oo-ui-fieldsetLayout.oo-ui-iconElement > .oo-ui-fieldsetLayout-header {position: relative;}.oo-ui-actionFieldLayout.oo-ui-fieldLayout-align-top .oo-ui-fieldLayout-header {padding-bottom: 6px !important;}.deputy.oo-ui-window {/** Place below default window manager */z-index: 199 !important;}";

    /**
     * Main class for CopiedTemplateEditor.
     */
    class CopiedTemplateEditor extends DeputyModule {
        constructor() {
            super(...arguments);
            this.static = CopiedTemplateEditor;
            this.CopiedTemplate = CopiedTemplate;
            /**
             * Whether the core has been loaded or not. Set to `true` here, since this is
             * obviously the core class.
             */
            this.loaded = true;
            /**
             * Pencil icon buttons on {{copied}} templates that open CTE.
             */
            this.startButtons = [];
        }
        /**
         * @inheritDoc
         */
        getName() {
            return 'ante';
        }
        /**
         * Perform actions that run *before* CTE starts (prior to execution). This involves
         * adding in necessary UI elements that serve as an entry point to CTE.
         */
        preInit() {
            const _super = Object.create(null, {
                preInit: { get: () => super.preInit }
            });
            return __awaiter(this, void 0, void 0, function* () {
                if (!(yield _super.preInit.call(this, deputyAnteEnglish))) {
                    return false;
                }
                if (
                // Button not yet appended
                document.getElementById('pt-cte') == null &&
                    // Not virtual namespace
                    mw.config.get('wgNamespaceNumber') >= 0) {
                    mw.util.addPortletLink('p-tb', '#', 
                    // Messages used here:
                    // * deputy.ante
                    // * deputy.ante.short
                    // * deputy.ante.acronym
                    mw.msg({
                        full: 'deputy.ante',
                        short: 'deputy.ante.short',
                        acronym: 'deputy.ante.acronym'
                    }[this.config.core.portletNames.get()]), 'pt-cte').addEventListener('click', (event) => {
                        event.preventDefault();
                        if (!event.currentTarget
                            .hasAttribute('disabled')) {
                            this.toggleButtons(false);
                            this.openEditDialog();
                        }
                    });
                }
                mw.loader.using(['oojs-ui-core', 'oojs-ui.styles.icons-editing-core'], () => {
                    // Only run if this script wasn't loaded using the loader.
                    if (!window.CopiedTemplateEditor || !window.CopiedTemplateEditor.loader) {
                        mw.hook('wikipage.content').add(() => {
                            // Find all {{copied}} templates and append our special button.
                            // This runs on the actual document, not the Parsoid document.
                            document.querySelectorAll([
                                'copiednotice', 'box-split-article', 'box-merged-from',
                                'box-merged-to', 'box-backwards-copy', 'box-translated-page'
                            ].map((v) => `.${v} > tbody > tr`).join(', '))
                                .forEach((e) => {
                                if (e.classList.contains('cte-upgraded')) {
                                    return;
                                }
                                e.classList.add('cte-upgraded');
                                const startButton = new OO.ui.ButtonWidget({
                                    icon: 'edit',
                                    title: mw.msg('deputy.ante.edit'),
                                    label: mw.msg('deputy.ante.edit')
                                }).setInvisibleLabel(true);
                                this.startButtons.push(startButton);
                                const td = document.createElement('td');
                                td.style.paddingRight = '0.9em';
                                td.appendChild(startButton.$element[0]);
                                e.appendChild(td);
                                startButton.on('click', () => {
                                    this.toggleButtons(false);
                                    this.openEditDialog();
                                });
                            });
                        });
                    }
                });
                this.startState = true;
                // Query parameter-based autostart
                if (/[?&]cte-autostart(=(1|yes|true|on)?(&|$)|$)/.test(window.location.search)) {
                    this.toggleButtons(false);
                    this.openEditDialog();
                }
                return true;
            });
        }
        /**
         * Opens the Copied Template Editor dialog.
         */
        openEditDialog() {
            mw.loader.using(CopiedTemplateEditor.dependencies, () => __awaiter(this, void 0, void 0, function* () {
                yield DeputyLanguage.loadMomentLocale();
                OO.ui.WindowManager.static.sizes.huge = {
                    width: 1100
                };
                mw.util.addCSS(cteStyles);
                yield WikiAttributionNotices.init();
                if (!this.dialog) {
                    // The following classes are used here:
                    // * deputy
                    // * copied-template-editor
                    this.dialog = CopiedTemplateEditorDialog({
                        main: this,
                        classes: [
                            // Attach "deputy" class if Deputy.
                            this.deputy ? 'deputy' : null,
                            'copied-template-editor'
                        ].filter((v) => !!v)
                    });
                    this.windowManager.addWindows([this.dialog]);
                }
                yield this.windowManager.openWindow(this.dialog).opened;
            }));
        }
        /**
         * Toggle the edit buttons.
         *
         * @param state The new state.
         */
        toggleButtons(state) {
            var _a;
            this.startState = state !== null && state !== void 0 ? state : !(this.startState || false);
            for (const button of this.startButtons) {
                button.setDisabled(state == null ? !button.isDisabled() : !state);
            }
            (_a = document.getElementById('.pt-cte a')) === null || _a === void 0 ? void 0 : _a.toggleAttribute('disabled', state);
        }
    }
    CopiedTemplateEditor.dependencies = [
        'moment',
        'oojs-ui-core',
        'oojs-ui-windows',
        'oojs-ui-widgets',
        'oojs-ui.styles.icons-accessibility',
        'oojs-ui.styles.icons-editing-core',
        'oojs-ui.styles.icons-editing-advanced',
        'oojs-ui.styles.icons-interactions',
        'ext.visualEditor.moduleIcons',
        'mediawiki.util',
        'mediawiki.api',
        'mediawiki.Title',
        'mediawiki.widgets',
        'mediawiki.widgets.DateInputWidget',
        'jquery.makeCollapsible'
    ];

    /**
     * Handles most recent page visits.
     */
    /**
     *
     */
    class Recents {
        /**
         * Saves the current page to the local list of most recently visited pages.
         */
        static save() {
            var _a;
            const page = normalizeTitle();
            if (page.getNamespaceId() === nsId('special') ||
                page.getNamespaceId() === nsId('media')) {
                // Don't save virtual namespaces.
                return;
            }
            const pageName = page.getPrefixedText();
            const recentsArray = (_a = JSON.parse(window.localStorage.getItem(Recents.key))) !== null && _a !== void 0 ? _a : [];
            if (recentsArray[0] === pageName) {
                // Avoid needless operations.
                return;
            }
            while (recentsArray.indexOf(pageName) !== -1) {
                recentsArray.splice(recentsArray.indexOf(pageName), 1);
            }
            if (recentsArray.length > 0) {
                recentsArray.pop();
            }
            recentsArray.splice(0, 0, pageName);
            window.localStorage.setItem(Recents.key, JSON.stringify(recentsArray));
        }
        /**
         * @return The most recently visited pages.
         */
        static get() {
            return JSON.parse(window.localStorage.getItem(Recents.key));
        }
    }
    Recents.key = 'mw-userjs-recents';

    var deputyAnnouncementsEnglish = {
    	"deputy.announcement.template.title": "Announcement title",
    	"deputy.announcement.template.message": "Announcement message",
    	"deputy.announcement.template.actionButton.label": "Button label",
    	"deputy.announcement.template.actionButton.title": "Button title"
    };

    /**
     *
     * Deputy announcements
     *
     * This will be loaded on all standalone modules and on main Deputy.
     * Be conservative with what you load!
     *
     */
    class DeputyAnnouncements {
        /**
         * Initialize announcements.
         * @param config
         */
        static init(config) {
            return __awaiter(this, void 0, void 0, function* () {
                yield Promise.all([
                    DeputyLanguage.load('shared', deputySharedEnglish),
                    DeputyLanguage.load('announcements', deputyAnnouncementsEnglish)
                ]);
                mw.util.addCSS('#siteNotice .deputy { text-align: left; }');
                for (const [id, announcements] of Object.entries(this.knownAnnouncements)) {
                    if (config.core.seenAnnouncements.get().includes(id)) {
                        continue;
                    }
                    if (announcements.expiry && (announcements.expiry < new Date())) {
                        // Announcement has expired. Skip it.
                        continue;
                    }
                    this.showAnnouncement(config, id, announcements);
                }
            });
        }
        /**
         *
         * @param config
         * @param announcementId
         * @param announcement
         */
        static showAnnouncement(config, announcementId, announcement) {
            mw.loader.using([
                'oojs-ui-core',
                'oojs-ui.styles.icons-interactions'
            ], () => {
                const messageWidget = DeputyMessageWidget({
                    classes: ['deputy'],
                    icon: 'feedback',
                    // Messages that can be used here:
                    // * deputy.announcement.<id>.title
                    title: mw.msg(`deputy.announcement.${announcementId}.title`),
                    // Messages that can be used here:
                    // * deputy.announcement.<id>.message
                    message: mw.msg(`deputy.announcement.${announcementId}.message`),
                    closable: true,
                    actions: announcement.actions.map(action => {
                        var _a;
                        const button = new OO.ui.ButtonWidget({
                            // Messages that can be used here:
                            // * deputy.announcement.<id>.<action id>.message
                            label: mw.msg(`deputy.announcement.${announcementId}.${action.id}.label`),
                            // Messages that can be used here:
                            // * deputy.announcement.<id>.<action id>.title
                            title: mw.msg(`deputy.announcement.${announcementId}.${action.id}.title`),
                            flags: (_a = action.flags) !== null && _a !== void 0 ? _a : []
                        });
                        button.on('click', action.action);
                        return button;
                    })
                });
                messageWidget.on('close', () => {
                    config.core.seenAnnouncements.set([...config.core.seenAnnouncements.get(), announcementId]);
                    config.save();
                });
                document.getElementById('siteNotice').appendChild(unwrapWidget(messageWidget));
            });
        }
    }
    DeputyAnnouncements.knownAnnouncements = {
    // No active announcements
    // 'announcementId': {
    // 	actions: [
    // 		{
    // 			id: 'actionButton',
    // 			flags: [ 'primary', 'progressive' ],
    // 			action: () => { /* do something */ }
    // 		}
    // 	]
    // }
    };

    /**
     * This function handles CTE loading when Deputy isn't present. When Deputy is not
     * present, the following must be done on our own:
     * (1) Instantiate an OOUI WindowManager
     * (2) Load language strings
     *
     * `preInit` handles all of those. This function simply calls it on run.
     *
     * @param window
     */
    ((window) => __awaiter(void 0, void 0, void 0, function* () {
        Recents.save();
        window.CopiedTemplateEditor = new CopiedTemplateEditor();
        yield window.CopiedTemplateEditor.preInit();
        yield DeputyAnnouncements.init(window.CopiedTemplateEditor.config);
    }))(window);

})();
// </nowiki>
// <3