"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.getPluginPageJSON=getPluginPageJSON;const tslib_1=require("tslib"),path_1=tslib_1.__importDefault(require("path")),common_1=require("../common"),schemaValidate_1=require("../../validate/schemaValidate"),locales_1=tslib_1.__importDefault(require("../../../../../utils/locales/locales")),common_2=require("../../../../../utils/common"),theme_1=require("../theme"),plugin_1=require("./plugin"),spreadUsingComponent=async(e,t)=>{const{project:a,root:i,filePath:n}=t,o=path_1.default.posix.relative(i,n);if(n.includes("miniprogram_npm/"))return;const r=await(0,plugin_1.getDevPluginJSON)(a,!0),s=Object.assign({},r.usingComponents);if(0!==Object.keys(s).length){e.usingComponents||(e.usingComponents={});for(const t in s){if(e.usingComponents[t])continue;const a=s[t]||"";if(a.startsWith("/")||a.startsWith("plugin://")){e.usingComponents[t]=a;continue}const i=path_1.default.posix.normalize(path_1.default.posix.relative(path_1.default.posix.dirname(o),a));e.usingComponents[t]=i.startsWith(".")?i:"./"+i}}};async function checkComponentPath(e,t){const{project:a,root:i,filePath:n}=t;await(0,common_1.checkComponentPath)({project:a,root:i,relativePath:path_1.default.posix.relative(i,n),inputJSON:e})}async function getPluginPageJSON(e){const{project:t}=e,{filePath:a}=e;if(!t.stat("",a))return{};const i=await t.getFile("",a),n=(0,common_1.checkJSONFormat)((0,common_2.checkUTF8)(i,a),a),o=await(0,theme_1.getPluginThemeLocation)(t);if(!o){const e=(0,common_1.getPageJSONVariableDecalearProperty)(n);e.length&&(0,common_2.throwError)({msg:'pluginJSON["themeLocation"] is required because:\n'+e.map(e=>`"${e.value}" as variable was declared at ${a}:${e.property}`).join("\n"),filePath:a})}let r={light:{},dark:{}};o&&(r=await(0,theme_1.checkThemeJSON)(t,{themeLocation:o,isPlugin:!0}));const s=(0,theme_1.mergeThemeJSONToPageJSON)(r,n,a),l=(0,schemaValidate_1.schemaValidate)("page",s.pageJSONLight),c=(0,schemaValidate_1.schemaValidate)("page",s.pageJSONDark);return l.warning&&(n.__warning__=locales_1.default.config.INVALID.format(l.warning)),o&&c.warning&&(n.__warning__=locales_1.default.config.INVALID.format(c.warning)),(0,schemaValidate_1.transValidateResult)(o?`${a} or ${o}["light"]`:a,l),o&&(0,schemaValidate_1.transValidateResult)(`${a} or ${o}["dark"]`,c),await checkComponentPath(n,e),await spreadUsingComponent(n,e),n}