"use strict";var t=this&&this.__awaiter||function(t,e,n,s){return new(n||(n=Promise))((function(i,o){function a(t){try{u(s.next(t))}catch(t){o(t)}}function c(t){try{u(s.throw(t))}catch(t){o(t)}}function u(t){var e;t.done?i(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(a,c)}u((s=s.apply(t,e||[])).next())}))};Object.defineProperty(exports,"__esModule",{value:!0});const e=require("./util"),n=require("../../util"),s=require("../../common/Constants"),i=require("../../tools/prompt/Prompt");function o(n){return t(this,void 0,void 0,(function*(){yield i.prompt.start({question:"package name",dValue:n,required:!0},new class{onSuccess(t){const s=(0,e.validateNameFun)(t,"init");s?a({name:s}):o(n)}})}))}function a(n){return t(this,void 0,void 0,(function*(){yield i.prompt.start({question:"version",dValue:"1.0.0",required:!0},new class{onSuccess(t){const s=(0,e.validateVersionFun)(t);s?c(Object.assign(Object.assign({},n),{version:s})):a(n)}})}))}function c(n){return t(this,void 0,void 0,(function*(){yield i.prompt.start({question:"description",dValue:"",required:!0},new class{onSuccess(s){s&&s.length>512?((0,e.output)("The description contains more than 512 characters."),c(n)):function(e){t(this,void 0,void 0,(function*(){yield i.prompt.start({question:"entry point",dValue:"index.ets",required:!0},new class{onSuccess(t){const n=t;u(Object.assign(Object.assign({},e),{main:n}))}})}))}(Object.assign(Object.assign({},n),{description:s}))}})}))}function u(n){return t(this,void 0,void 0,(function*(){yield i.prompt.start({question:"author",dValue:"",required:!1},new class{onSuccess(t){let s,i=t.trim();s=!i||(i.toLowerCase().includes("name:")||i.toLowerCase().includes("email:")?(0,e.validateAuthorFun)(i):(0,e.validateAuthorStringFun)(i)),s?(i=!0===s?"":s,r(Object.assign(Object.assign({},n),{author:i}))):u(n)}})}))}function r(o){return t(this,void 0,void 0,(function*(){yield i.prompt.start({question:"license",dValue:"ISC",required:!0},new class{onSuccess(a){const c=(0,e.validateLicenseFun)(a);if(c.type){const a=JSON.stringify(Object.assign(Object.assign({},o),{license:c.license,dependencies:{}}),null," ");(0,e.output)(`About to write to ${process.cwd()}\\${s.Constants.MyPackageJson}:\n`),(0,e.output)(`${a}\n`),function(o){t(this,void 0,void 0,(function*(){yield i.prompt.start({question:"Is this OK?",dValue:"yes",required:!0},new class{onSuccess(t){"yes"!==t.toLowerCase()&&"y"!==t.toLowerCase()||(n.FsBlockingUtil.writeFileSync(`${s.Constants.MyPackageJson}`,o),(0,e.output)(`\n${s.Constants.MyPackageJson} file is created success.`)),i.prompt.tearDown()}})}))}(a)}else r(o)}})}))}exports.default=function(n){return t(this,void 0,void 0,(function*(){(0,e.output)(`This utility will walk you through creating a ${s.Constants.MyPackageJson} file.\nIt only covers the most common items, and tries to guess sensible defaults.\n\nSee ${s.Constants.PM} help init for definitive documentation on these fields\nand exactly what they do.\n\nUse ${s.Constants.PM} install <pkg> afterwards to install a package and\nsave it as a dependency in the ${s.Constants.MyPackageJson} file.\n\nPress ^C at any time to quit.`),yield o(n)}))};