"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const type_analyzer_1 = require("@previewjs/type-analyzer");
const vfs_1 = require("@previewjs/vfs");
const path_1 = __importDefault(require("path"));
const vitest_1 = require("vitest");
const _1 = require(".");
const extract_component_1 = require("./extract-component");
const vue_reader_1 = require("./vue-reader");
const ROOT_DIR = path_1.default.join(__dirname, "virtual");
const MAIN_FILE_TSX = path_1.default.join(ROOT_DIR, "App.tsx");
const MAIN_FILE_VUE = path_1.default.join(ROOT_DIR, "MyComponent.vue");
const STORIES_FILE = path_1.default.join(ROOT_DIR, "App.stories.tsx");
(0, vitest_1.describe)("extractVueComponents", () => {
    let memoryReader;
    let typeAnalyzer;
    (0, vitest_1.beforeEach)(async () => {
        memoryReader = (0, vfs_1.createMemoryReader)();
        memoryReader.updateFile(MAIN_FILE_VUE, `
<template>
  <div>
    Hello, World!
  </div>
</template>

<script>
export default {
  name: "App",
};
</script>
`);
        const frameworkPlugin = await _1.vue2FrameworkPlugin.create();
        const rootDirPath = path_1.default.join(__dirname, "virtual");
        const reader = (0, vfs_1.createStackedReader)([
            (0, vue_reader_1.createVueTypeScriptReader)(memoryReader),
            (0, vfs_1.createFileSystemReader)({
                mapping: {
                    from: path_1.default.join(__dirname, "..", "preview", "modules"),
                    to: path_1.default.join(rootDirPath, "node_modules"),
                },
                watch: false,
            }),
            (0, vfs_1.createFileSystemReader)({
                watch: false,
            }), // required for TypeScript libs, e.g. Promise
        ]);
        typeAnalyzer = (0, type_analyzer_1.createTypeAnalyzer)({
            rootDirPath,
            reader,
            tsCompilerOptions: frameworkPlugin.tsCompilerOptions,
        });
    });
    (0, vitest_1.afterEach)(() => {
        typeAnalyzer.dispose();
    });
    (0, vitest_1.it)("detects expected components", async () => {
        memoryReader.updateFile(MAIN_FILE_TSX, `
const Component1 = () => {
  return <div>Hello, World!</div>;
};

function Component2() {
  return <div>Hello, World!</div>;
};

export const NotAStory = {
  args: {}
};

export default Component1;
      
`);
        (0, vitest_1.expect)(extract(MAIN_FILE_TSX)).toMatchObject([
            {
                name: "Component1",
                info: {
                    kind: "component",
                    exported: true,
                },
            },
            {
                name: "Component2",
                info: {
                    kind: "component",
                    exported: false,
                },
            },
        ]);
    });
    (0, vitest_1.it)("detects components without any Vue import", async () => {
        memoryReader.updateFile(MAIN_FILE_TSX, `
export function DeclaredFunction() {
  return <div>Hello, World!</div>;
}

const ConstantFunction = () => <div>Hello, World!</div>;
`);
        (0, vitest_1.expect)(extract(MAIN_FILE_TSX)).toMatchObject([
            {
                name: "DeclaredFunction",
                info: {
                    kind: "component",
                    exported: true,
                },
            },
            {
                name: "ConstantFunction",
                info: {
                    kind: "component",
                    exported: false,
                },
            },
        ]);
    });
    (0, vitest_1.it)("detects default export component (arrow function)", async () => {
        memoryReader.updateFile(MAIN_FILE_TSX, `
export default () => {
  return <div>Hello, World!</div>;
}
`);
        (0, vitest_1.expect)(extract(MAIN_FILE_TSX)).toMatchObject([
            {
                name: "default",
                info: {
                    kind: "component",
                    exported: true,
                },
            },
        ]);
    });
    (0, vitest_1.it)("detects default export component (named function)", async () => {
        memoryReader.updateFile(MAIN_FILE_TSX, `
export default function test(){
  return <div>Hello, World!</div>;
}
`);
        (0, vitest_1.expect)(extract(MAIN_FILE_TSX)).toMatchObject([
            {
                name: "test",
                info: {
                    kind: "component",
                    exported: true,
                },
            },
        ]);
    });
    (0, vitest_1.it)("detects default export component (anonymous function)", async () => {
        memoryReader.updateFile(MAIN_FILE_TSX, `
export default function(){
  return <div>Hello, World!</div>;
}
`);
        (0, vitest_1.expect)(extract(MAIN_FILE_TSX)).toMatchObject([
            {
                name: "default",
                info: {
                    kind: "component",
                    exported: true,
                },
            },
        ]);
    });
    (0, vitest_1.it)("detects CSF1 stories", async () => {
        memoryReader.updateFile(STORIES_FILE, `
import Button from "./MyComponent.vue";

export default {
  component: Button
}

export const Primary = () => ({
  components: { Button },
  template: '<Button primary label="Button" />',
});
`);
        (0, vitest_1.expect)(extract(STORIES_FILE)).toMatchObject([
            {
                name: "Primary",
                info: {
                    // TODO: this should be "story".
                    kind: "component",
                    exported: true,
                },
            },
        ]);
    });
    (0, vitest_1.it)("detects CSF2 stories", async () => {
        memoryReader.updateFile(STORIES_FILE, `
import Button from "./MyComponent.vue";

export default {
  component: Button
}

const Template = (args, { argTypes }) => ({
  props: Object.keys(argTypes),
  components: { Button },
});

export const Primary = Template.bind({});

Primary.args = {
  primary: true,
  label: 'Button',
};
`);
        (0, vitest_1.expect)(extract(STORIES_FILE)).toMatchObject([
            {
                name: "Primary",
                info: {
                    kind: "story",
                    associatedComponent: {
                        absoluteFilePath: MAIN_FILE_VUE,
                        name: "MyComponent",
                    },
                },
            },
        ]);
    });
    (0, vitest_1.it)("detects CSF3 stories", async () => {
        memoryReader.updateFile(STORIES_FILE, `
import Button from './MyComponent.vue';

export default {
  component: Button
}

export const Example = {
  args: {
    label: "Hello, World!"
  }
}
export const NoArgs = {}
export function NotStory() {}
`);
        (0, vitest_1.expect)(extract(STORIES_FILE)).toMatchObject([
            {
                name: "Example",
                info: {
                    kind: "story",
                    associatedComponent: {
                        absoluteFilePath: MAIN_FILE_VUE,
                        name: "MyComponent",
                    },
                },
            },
            {
                name: "NoArgs",
                info: {
                    kind: "story",
                    associatedComponent: {
                        absoluteFilePath: MAIN_FILE_VUE,
                        name: "MyComponent",
                    },
                },
            },
        ]);
    });
    function extract(absoluteFilePath) {
        return (0, extract_component_1.extractVueComponents)(typeAnalyzer.analyze([absoluteFilePath]), absoluteFilePath);
    }
});
