'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.htmlParser = exports.styleParser = exports.scriptParser = exports.layoutParser = exports.componentParser = undefined;

var _fs = require('fs');

var _fs2 = _interopRequireDefault(_fs);

var _models = require('../models');

var _camelCase = require('camel-case');

var _camelCase2 = _interopRequireDefault(_camelCase);

var _style = require('./style');

var _style2 = _interopRequireDefault(_style);

var _html = require('./html');

var _html2 = _interopRequireDefault(_html);

var _script = require('./script');

var _script2 = _interopRequireDefault(_script);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var htmlRegex = /(<template.*?>)([\s\S]*)(<\/template>)/gm;
var scriptRegex = /(<script.*?>)([\s\S]*?)(<\/script>)/gm;
var styleRegex = /(<style.*?>)([\s\S]*?)(<\/style>)/gm;

function layoutParser(layoutPath, defaults, type) {
    return new Promise(function (resolve) {
        _fs2.default.readFile(layoutPath, 'utf-8', function (err, content) {
            if (err) {
                content = defaults.backupLayout;
                // let error = `Could not find the layout, I was expecting it to live here
                // ${layoutPath}
                // But I couldn't find it there ¯\_(ツ)_/¯
                // So I'm using the default layout`;
                // console.warn(error)
            }

            var body = (0, _html2.default)(content, htmlRegex, false);
            content = content.replace(htmlRegex, '');

            var script = (0, _script2.default)(content, defaults, type, scriptRegex);

            resolve({
                type: type,
                template: body,
                script: script
            });
        });
    });
}

function componentParser(templatePath, defaults, type) {
    return new Promise(function (resolve, reject) {
        _fs2.default.readFile(templatePath, 'utf-8', function (err, content) {
            if (err) {
                var error = 'Could Not Find Component, I was expecting it to live here \n' + templatePath + ' \nBut I couldn\'t find it there, \xAF\\_(\u30C4)_/\xAF\n\n';
                console.error(new Error(error));
                reject(error);
            } else {
                var body = (0, _html2.default)(content, htmlRegex, true);
                content = content.replace(htmlRegex, '');

                var script = (0, _script2.default)(content, defaults, type, scriptRegex);
                var templateArray = templatePath.split('/');

                var style = (0, _style2.default)(content, styleRegex);
                content = content.replace(styleRegex, '');

                if (templateArray.length === 0) {
                    var _error = 'I had an error processing component templates. in this file \n' + templatePath;
                    console.error(new Error(_error));
                    reject(_error);
                }

                var templateName = templateArray[templateArray.length - 1].replace('.vue', '');
                var componentScript = script || {};
                componentScript.template = body;

                resolve({
                    type: type,
                    style: style,
                    name: (0, _camelCase2.default)(templateName),
                    script: componentScript
                });
            }
        });
    });
}

exports.componentParser = componentParser;
exports.layoutParser = layoutParser;
exports.scriptParser = _script2.default;
exports.styleParser = _style2.default;
exports.htmlParser = _html2.default;