"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.glob=glob;const tslib_1=require("tslib"),path_1=tslib_1.__importDefault(require("path")),minimatch_1=require("minimatch"),fs_1=require("./fs"),tools_1=require("./tools"),log=tslib_1.__importStar(require("./log"));function ignoreMap(t){let e=null;if(t.endsWith("/**")){const r=t.replace(/(\/\*\*)+$/,"");e=new minimatch_1.Minimatch(r,{dot:!0})}return{matcher:new minimatch_1.Minimatch(t,{dot:!0}),gmatcher:e}}async function readdir(t,e){const{ignore:r,cwd:i}=e;return(await fs_1.fsAgent.readdir(path_1.default.posix.join(i,t))).filter(e=>{const i=(0,tools_1.normalizePath)(path_1.default.posix.join(t,e)),n=isIgnored(i,r);return n&&log.debug(`file '${i}' has been ignore`),!n})}function childrenIgnored(t,e){return!!e.length&&e.some(e=>{var r;return!!(null===(r=e.gmatcher)||void 0===r?void 0:r.match(t))})}function isIgnored(t,e){return!!e.length&&e.some(e=>{var r;return e.matcher.match(t)||!!(null===(r=e.gmatcher)||void 0===r?void 0:r.match(t))})}async function _glob(t,e){const{cwd:r,ignore:i,dir:n}=e,o=path_1.default.posix.join(r,t),a=await fs_1.fsAgent.stat(o);if(!a)return[];if(a.isFile())return[(0,tools_1.normalizePath)(t)];if(a.isDirectory()){if(childrenIgnored(t,i))return log.debug(`directory '${o}' has been ignore`),[];let r=[];n&&t&&r.push((0,tools_1.normalizePath)(t+"/"));const a=await readdir(t,e);for(const i of a){const n=await _glob(path_1.default.posix.join(t,i),e);r=r.concat(n)}return r}return[]}async function glob(t){const{cwd:e,ignore:r,dir:i=!1}=t;let n=[];return Array.isArray(r)&&(n=r.map(ignoreMap)),_glob("",{cwd:e,ignore:n,dir:i})}