var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
/* eslint-disable no-param-reassign */
import React from 'react';
import { toId } from '@storybook/csf';
import { Table, TableRow } from '@workday/canvas-kit-react/table';
import { Hyperlink } from '@workday/canvas-kit-react/button';
import { specifications } from './specs';
import { GithubBranch, GithubUrl, StorybookUrl } from './docs';
function createTableRows(rows, item, index, children, context) {
    if (context === void 0) { context = { given: [], when: [], then: [], prefix: '' }; }
    if (item.type === 'describe') {
        if (/^given/i.test(item.name)) {
            context = __assign(__assign({}, context), { given: context.given.concat(context.prefix ? context.prefix + " " + item.name : item.name) });
            context.prefix = '';
        }
        else if (/^[wt]hen/i.test(item.name)) {
            context = __assign(__assign({}, context), { when: context.when.concat(context.prefix ? context.prefix + " " + item.name : item.name) });
            context.prefix = '';
        }
        else {
            context = __assign(__assign({}, context), { prefix: context.prefix ? context.prefix + " " + item.name : item.name });
        }
        item.children.reduce(function (rows, item, index, children) { return createTableRows(rows, item, index, children, context); }, rows);
    }
    else {
        context = __assign(__assign({}, context), { then: context.then.concat(context.prefix ? context.prefix + " " + item.name : item.name) });
        rows.push(context);
    }
    return rows;
}
export var Specifications = function (_a) {
    var file = _a.file, name = _a.name;
    var storybookBaseUrl = React.useContext(StorybookUrl);
    var githubUrl = React.useContext(GithubUrl);
    var githubBranch = React.useContext(GithubBranch);
    var specFile = specifications.find(function (f) { return f.name === file; });
    if (!specFile) {
        return null;
    }
    var block = name ? specFile.children.find(function (d) { return d.name === name; }) : specFile.children[0];
    if (!block) {
        return null;
    }
    var renderGiven = function (input) {
        if (!input) {
            return "Could not find a \"given\". Check the spec file.";
        }
        var matches = input.match(/(.*)(\[[A-Za-z/\s]+), ([A-Za-z\s]+)\](.*)/);
        if (matches == null) {
            return input;
        }
        var first = matches[1], kind = matches[2], name = matches[3], last = matches[4];
        return React.createElement(React.Fragment, null,
            first.replace(/given /i, ''),
            React.createElement(Hyperlink, { href: storybookBaseUrl + "?path=/story/" + toId(kind, name.replace('DefaultStory', 'Default Story')) }, name.replace('DefaultStory', 'Default')),
            last);
    };
    return block.type === 'describe' ? (React.createElement(React.Fragment, null,
        React.createElement(Table, null,
            React.createElement("thead", null,
                React.createElement(TableRow, { header: true },
                    React.createElement("th", null, "Given"),
                    React.createElement("th", null, "When"),
                    React.createElement("th", null, "Then"))),
            React.createElement("tbody", null, block.children.reduce(createTableRows, []).map(function (row, index) { return (React.createElement(TableRow, { key: index },
                React.createElement("td", null, renderGiven(row.given[0])),
                React.createElement("td", null,
                    React.createElement("ul", null, row.when.map(function (item, index) { return (React.createElement("li", { key: index }, (index !== 0 ? 'AND THEN ' : '') + item.replace(/[tw]hen /i, ''))); }))),
                React.createElement("td", null,
                    React.createElement("ul", null, row.then.map(function (item, index) { return (React.createElement("li", { key: index }, (item.indexOf('should') === 0 ? 'it ' : '') + item)); }))))); }))),
        "Source:",
        ' ',
        React.createElement(Hyperlink, { href: githubUrl + "blob/" + githubBranch + "/cypress/integration/" + file }, file))) : null;
};
