import { MenuSubsectionsBySection } from '@0xproject/react-shared';
import compareVersions = require('compare-versions');
import * as _ from 'lodash';

import {
    ContractsByVersionByNetworkId,
    DocAgnosticFormat,
    DocsInfoConfig,
    DocsInfoTypeConfigs,
    DocsMenu,
    DoxityDocObj,
    SectionsMap,
    SupportedDocJson,
    TypeDocNode,
} from './types';
import { doxityUtils } from './utils/doxity_utils';
import { typeDocUtils } from './utils/typedoc_utils';

export class DocsInfo {
    public id: string;
    public type: SupportedDocJson;
    public displayName: string;
    public packageUrl: string;
    public menu: DocsMenu;
    public sections: SectionsMap;
    public sectionNameToMarkdown: { [sectionName: string]: string };
    public contractsByVersionByNetworkId?: ContractsByVersionByNetworkId;
    public typeConfigs: DocsInfoTypeConfigs;
    private _docsInfo: DocsInfoConfig;
    constructor(config: DocsInfoConfig) {
        this.id = config.id;
        this.type = config.type;
        this.displayName = config.displayName;
        this.packageUrl = config.packageUrl;
        this.sections = config.sections;
        this.sectionNameToMarkdown = config.sectionNameToMarkdown;
        this.contractsByVersionByNetworkId = config.contractsByVersionByNetworkId;
        this.typeConfigs = config.typeConfigs;
        this._docsInfo = config;
    }
    public isPublicType(typeName: string): boolean {
        if (_.isUndefined(this._docsInfo.typeConfigs.publicTypes)) {
            return false;
        }
        const isPublic = _.includes(this._docsInfo.typeConfigs.publicTypes, typeName);
        return isPublic;
    }
    public getModulePathsIfExists(sectionName: string): string[] {
        const modulePathsIfExists = this._docsInfo.sectionNameToModulePath[sectionName];
        return modulePathsIfExists;
    }
    public getMenu(selectedVersion?: string): { [section: string]: string[] } {
        if (_.isUndefined(selectedVersion) || _.isUndefined(this._docsInfo.menuSubsectionToVersionWhenIntroduced)) {
            return this._docsInfo.menu;
        }

        const finalMenu = _.cloneDeep(this._docsInfo.menu);
        if (_.isUndefined(finalMenu.contracts)) {
            return finalMenu;
        }

        // TODO: refactor to include more sections then simply the `contracts` section
        finalMenu.contracts = _.filter(finalMenu.contracts, (contractName: string) => {
            const versionIntroducedIfExists = this._docsInfo.menuSubsectionToVersionWhenIntroduced[contractName];
            if (!_.isUndefined(versionIntroducedIfExists)) {
                const existsInSelectedVersion = compareVersions(selectedVersion, versionIntroducedIfExists) >= 0;
                return existsInSelectedVersion;
            } else {
                return true;
            }
        });
        return finalMenu;
    }
    public getMenuSubsectionsBySection(docAgnosticFormat?: DocAgnosticFormat): MenuSubsectionsBySection {
        const menuSubsectionsBySection = {} as MenuSubsectionsBySection;
        if (_.isUndefined(docAgnosticFormat)) {
            return menuSubsectionsBySection;
        }

        const docSections = _.keys(this.sections);
        _.each(docSections, sectionName => {
            const docSection = docAgnosticFormat[sectionName];
            if (_.isUndefined(docSection)) {
                return; // no-op
            }

            if (!_.isUndefined(this.sections.types) && sectionName === this.sections.types) {
                const sortedTypesNames = _.sortBy(docSection.types, 'name');
                const typeNames = _.map(sortedTypesNames, t => t.name);
                menuSubsectionsBySection[sectionName] = typeNames;
            } else {
                let eventNames: string[] = [];
                if (!_.isUndefined(docSection.events)) {
                    const sortedEventNames = _.sortBy(docSection.events, 'name');
                    eventNames = _.map(sortedEventNames, m => m.name);
                }
                const sortedMethodNames = _.sortBy(docSection.methods, 'name');
                const methodNames = _.map(sortedMethodNames, m => m.name);
                menuSubsectionsBySection[sectionName] = [...methodNames, ...eventNames];
            }
        });
        return menuSubsectionsBySection;
    }
    public getTypeDefinitionsByName(docAgnosticFormat: DocAgnosticFormat) {
        if (_.isUndefined(this.sections.types)) {
            return {};
        }

        const typeDocSection = docAgnosticFormat[this.sections.types];
        const typeDefinitionByName = _.keyBy(typeDocSection.types, 'name');
        return typeDefinitionByName;
    }
    public isVisibleConstructor(sectionName: string): boolean {
        return _.includes(this._docsInfo.visibleConstructors, sectionName);
    }
    public convertToDocAgnosticFormat(docObj: DoxityDocObj | TypeDocNode): DocAgnosticFormat {
        if (this.type === SupportedDocJson.Doxity) {
            return doxityUtils.convertToDocAgnosticFormat(docObj as DoxityDocObj);
        } else {
            return typeDocUtils.convertToDocAgnosticFormat(docObj as TypeDocNode, this);
        }
    }
}
ss='tabs'><tr><td>
<a href='/~lantw44/cgit/freebsd-ports-gnome/about/?h=dependabot/npm_and_yarn/devel/electron6/files/lodash.merge-4.6.2'>about</a><a href='/~lantw44/cgit/freebsd-ports-gnome/?h=dependabot/npm_and_yarn/devel/electron6/files/lodash.merge-4.6.2'>summary</a><a href='/~lantw44/cgit/freebsd-ports-gnome/refs/?h=dependabot/npm_and_yarn/devel/electron6/files/lodash.merge-4.6.2&amp;id=9debac461b7729b783ca5a0f5e46461ddac07173'>refs</a><a href='/~lantw44/cgit/freebsd-ports-gnome/log/russian/p5-Lingua-RU-Charset/pkg-plist?h=dependabot/npm_and_yarn/devel/electron6/files/lodash.merge-4.6.2'>log</a><a href='/~lantw44/cgit/freebsd-ports-gnome/tree/russian/p5-Lingua-RU-Charset/pkg-plist?h=dependabot/npm_and_yarn/devel/electron6/files/lodash.merge-4.6.2&amp;id=9debac461b7729b783ca5a0f5e46461ddac07173'>tree</a><a href='/~lantw44/cgit/freebsd-ports-gnome/commit/russian/p5-Lingua-RU-Charset/pkg-plist?h=dependabot/npm_and_yarn/devel/electron6/files/lodash.merge-4.6.2&amp;id=9debac461b7729b783ca5a0f5e46461ddac07173'>commit</a><a class='active' href='/~lantw44/cgit/freebsd-ports-gnome/diff/russian/p5-Lingua-RU-Charset/pkg-plist?h=dependabot/npm_and_yarn/devel/electron6/files/lodash.merge-4.6.2&amp;id=9debac461b7729b783ca5a0f5e46461ddac07173'>diff</a><a href='/~lantw44/cgit/freebsd-ports-gnome/stats/russian/p5-Lingua-RU-Charset/pkg-plist?h=dependabot/npm_and_yarn/devel/electron6/files/lodash.merge-4.6.2'>stats</a></td><td class='form'><form class='right' method='get' action='/~lantw44/cgit/freebsd-ports-gnome/log/russian/p5-Lingua-RU-Charset/pkg-plist'>
<input type='hidden' name='h' value='dependabot/npm_and_yarn/devel/electron6/files/lodash.merge-4.6.2'/><input type='hidden' name='id' value='9debac461b7729b783ca5a0f5e46461ddac07173'/><select name='qt'>
<option value='grep'>log msg</option>
<option value='author'>author</option>
<option value='committer'>committer</option>
<option value='range'>range</option>
</select>
<input class='txt' type='search' size='10' name='q' value=''/>
<input type='submit' value='search'/>
</form>
</td></tr></table>
<div class='path'>path: <a href='/~lantw44/cgit/freebsd-ports-gnome/diff/?h=dependabot/npm_and_yarn/devel/electron6/files/lodash.merge-4.6.2&amp;id=9debac461b7729b783ca5a0f5e46461ddac07173'>root</a>/<a href='/~lantw44/cgit/freebsd-ports-gnome/diff/russian?h=dependabot/npm_and_yarn/devel/electron6/files/lodash.merge-4.6.2&amp;id=9debac461b7729b783ca5a0f5e46461ddac07173'>russian</a>/<a href='/~lantw44/cgit/freebsd-ports-gnome/diff/russian/p5-Lingua-RU-Charset?h=dependabot/npm_and_yarn/devel/electron6/files/lodash.merge-4.6.2&amp;id=9debac461b7729b783ca5a0f5e46461ddac07173'>p5-Lingua-RU-Charset</a>/<a href='/~lantw44/cgit/freebsd-ports-gnome/diff/russian/p5-Lingua-RU-Charset/pkg-plist?h=dependabot/npm_and_yarn/devel/electron6/files/lodash.merge-4.6.2&amp;id=9debac461b7729b783ca5a0f5e46461ddac07173'>pkg-plist</a></div><div class='content'><div class='cgit-panel'><b>diff options</b><form method='get'><input type='hidden' name='h' value='dependabot/npm_and_yarn/devel/electron6/files/lodash.merge-4.6.2'/><input type='hidden' name='id' value='9debac461b7729b783ca5a0f5e46461ddac07173'/><table><tr><td colspan='2'/></tr><tr><td class='label'>context:</td><td class='ctrl'><select name='context' onchange='this.form.submit();'><option value='1'>1</option><option value='2'>2</option><option value='3' selected='selected'>3</option><option value='4'>4</option><option value='5'>5</option><option value='6'>6</option><option value='7'>7</option><option value='8'>8</option><option value='9'>9</option><option value='10'>10</option><option value='15'>15</option><option value='20'>20</option><option value='25'>25</option><option value='30'>30</option><option value='35'>35</option><option value='40'>40</option></select></td></tr><tr><td class='label'>space:</td><td class='ctrl'><select name='ignorews' onchange='this.form.submit();'><option value='0' selected='selected'>include</option><option value='1'>ignore</option></select></td></tr><tr><td class='label'>mode:</td><td class='ctrl'><select name='dt' onchange='this.form.submit();'><option value='0' selected='selected'>unified</option><option value='1'>ssdiff</option><option value='2'>stat only</option></select></td></tr><tr><td/><td class='ctrl'><noscript><input type='submit' value='reload'/></noscript></td></tr></table></form></div><div class='diffstat-header'><a href='/~lantw44/cgit/freebsd-ports-gnome/diff/?h=dependabot/npm_and_yarn/devel/electron6/files/lodash.merge-4.6.2&amp;id=9debac461b7729b783ca5a0f5e46461ddac07173'>Diffstat</a> (limited to 'russian/p5-Lingua-RU-Charset/pkg-plist')</div><table summary='diffstat' class='diffstat'>