/**
 * Copyright © 2019 Johnson & Johnson
 *
 * 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.
 */
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
import path from 'path';
import fs from 'fs-extra';
var niceName = function (s) { return s
    .replace(/[a-z0-9]([A-Z])/g, function (m) { return "".concat(m[0], "_").concat(m[1]); })
    .replace(/_/g, ' ')
    .split(' ')
    .map(function (word) { return "".concat(word.slice(0, 1).toUpperCase()).concat(word.slice(1)); })
    .join(' '); };
/**
 * createBar works to generate markdown used in _sidebar and _navbar from
 * a tree (paths param).  It used parents and depth to allow recursion.
 *
 * @param paths what is the tree we should transform
 * @param parent what are the current parent folders we are in
 * @param depth how many more times to recurse in the tree
 */
var createBar = function (paths, parent, depth, writeHeader, indent) {
    if (writeHeader === void 0) { writeHeader = true; }
    if (indent === void 0) { indent = 1; }
    var pathPrefix = "/".concat(parent.map(function (item) { return "".concat(item, "/"); }).join(''));
    var linePrefix = ' '.repeat(indent * 2);
    var markdownPrefix = '-';
    var header = writeHeader && parent.length > 0 && parent.slice(-1)[0];
    var navbar = header ? "".concat(markdownPrefix, " ").concat(niceName(header.replace(/.md$/, '')), "\n") : '';
    Object.keys(paths).forEach(function (key) {
        var line = '';
        var link = key.replace(/.md$/, '');
        var name = niceName(link);
        if (typeof paths[key] === 'object') {
            var branch = paths[key];
            var subLines = depth > 0 || !branch['README.md'] ? createBar(paths[key], __spreadArray(__spreadArray([], parent, true), [key], false), depth - 1, false, indent + 1) : '';
            if (branch['README.md']) {
                line = "".concat(linePrefix).concat(markdownPrefix, " [").concat(name, "](").concat(pathPrefix).concat(link, "/)\n");
                // If we have subLines but we have no README then do not add the tree item
            }
            else if (subLines) {
                line = "".concat(linePrefix).concat(markdownPrefix, " ").concat(name, "\n");
            }
            line += subLines;
        }
        else if (key !== 'README.md' && path.extname(paths[key]) === '.md') {
            line = "".concat(linePrefix).concat(markdownPrefix, " [").concat(name, "](").concat(pathPrefix).concat(link, ")\n");
        }
        navbar += line;
    });
    return navbar;
};
/**
 * writeSideBar returns a promise to write a _sidebar file it resolves void
 * @param loc where to write the _sidebar.md
 * @param tree what tree to transform
 * @param parents what parents should be use in the paths of the items
 */
export var writeSideBar = function (loc, tree, parents) { return (
/* Depth is unlimited as we want everyhing in main sidebar -- so set to 100 */
fs.writeFile(path.join(loc, '_sidebar.md'), createBar(tree, parents, 100, true))); };
/**
 * writeSideBars retruns a promise to write a sidebar for each in the tree
 * it resolves to a void
 * @param loc
 * @param tree
 */
export var writeSideBars = function (loc, tree) { return (writeSideBar(loc, tree, [])); };
export default createBar;
//# sourceMappingURL=createBar.js.map