"use strict";
/*
 * Copyright © 2020 Atomist, Inc.
 *
 * 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.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.isXmldocTreeNode = exports.XmldocFileParser = void 0;
const logger_1 = require("@atomist/automation-client/lib/util/logger");
const xmldoc_1 = require("xmldoc");
/**
 * FileParser implementation that uses xmldoc library.
 * Preserves and exposes positions.
 */
class XmldocFileParser {
    constructor() {
        this.rootName = "xml";
    }
    async toAst(f) {
        try {
            const content = await f.getContent();
            const document = new xmldoc_1.XmlDocument(content);
            return new XmldocTreeNodeImpl(document, undefined, content);
        }
        catch (err) {
            logger_1.logger.warn("Could not parse XML document at '%s'", f.path, err);
            return undefined;
        }
    }
}
exports.XmldocFileParser = XmldocFileParser;
function isXmldocTreeNode(tn) {
    const maybe = tn;
    return !!maybe.innerValue;
}
exports.isXmldocTreeNode = isXmldocTreeNode;
class XmldocTreeNodeImpl {
    constructor(xd, $parent, rawDoc) {
        this.xd = xd;
        this.$parent = $parent;
        this.rawDoc = rawDoc;
        // Add attributes to this
        for (const propName of Object.getOwnPropertyNames(this.xd.attr)) {
            this[propName] = this.xd.attr[propName];
        }
    }
    get $children() {
        return this.xd.children
            .filter(kid => kid.type === "element")
            .map(k => new XmldocTreeNodeImpl(k, this, this.rawDoc));
    }
    get $name() {
        return this.xd.name;
    }
    get $offset() {
        return this.xd.startTagPosition - 1;
    }
    /**
     * This is the full element value
     * @return {string}
     */
    get $value() {
        // toString may not be accurate, as per xmldoc readme, but we can work with it
        // as the offset will be accurate
        const fromXmldocToString = this.xd.toString({ preserveWhitespace: true, compressed: false, trimmed: false });
        const fromRawDoc = this.rawDoc.substr(this.$offset, fromXmldocToString.length);
        if (fromRawDoc !== fromXmldocToString) {
            // In this case, check we have all the non whitespace characters from the toString value
            const nonWhitespaceCount = fromXmldocToString.replace(/\s+/g, "").length;
            let included = 0;
            let str = "";
            for (let i = 0; i < fromXmldocToString.length && included < nonWhitespaceCount; i++) {
                const c = this.rawDoc.substr(this.$offset).charAt(i);
                if (!["\n", " ", "\t", "\r"].includes(c)) {
                    ++included;
                }
                str += c;
            }
            return str;
        }
        return fromXmldocToString;
    }
    get innerValue() {
        return this.xd.val;
    }
}
//# sourceMappingURL=XmldocFileParser.js.map