<%*

// 필요한 변수들을 설정합니다.
let firstOrLastLine = 'last';
let qcFileName = tp.date.now("YYYY-MM-DD(ddd)");
let sectionName = '## 📒 Memo';
let folderOverride = '20_Planner/21_Daily';

// 현재 날짜와 시간을 설정합니다.
let curDateFormat = tp.date.now("YYYY-MM-DD(ddd)");
let finalTimestamp = curDateFormat;
let curTimeFormat = tp.date.now("hh:mm A");
if(curTimeFormat != '') {
    finalTimestamp = finalTimestamp + ' ' + curTimeFormat;
}

// 폴더 위치를 설정합니다.
let qcFolderLocation;
if(folderOverride) {
    qcFolderLocation = folderOverride;
} else {
    if(this.app.vault.config.newFileLocation != 'current') {
        qcFolderLocation = this.app.fileManager.getNewFileParent().path;
    } else {
        qcFolderLocation = '/';
    }
}
if(qcFolderLocation != '') {
    qcFolderLocation = qcFolderLocation + '/';
}
qcFolderLocation = qcFolderLocation.replace(/\/\//g,'/');
if(qcFolderLocation == '/') {
    qcFolderLocation = '';
}
if(qcFolderLocation.startsWith('/')) {
    qcFolderLocation = qcFolderLocation.substring(1);
}

// 파일 경로를 설정하고, 파일이 있다면 노트를 추가합니다.
let qcFilePath = qcFolderLocation + qcFileName + '.md';
let qcFile = this.app.vault.getAbstractFileByPath(qcFilePath);

if(qcFile) {
    let qcNote = await tp.system.prompt("Enter a Quick Capture note");
    let isTodo = qcNote.startsWith('!');
    let finalNote = '- ' + (isTodo ? '- [ ] ' : '') + finalTimestamp + ' - ' + (isTodo ? qcNote.substring(1) : qcNote);
    let curContent = await this.app.vault.read(qcFile);
    let newContents;
    if(curContent.includes(sectionName)){
        let sectionIndex = curContent.indexOf(sectionName) + sectionName.length;
        if(curContent.charAt(sectionIndex + 1) === '\n'){
            newContents = curContent.slice(0, sectionIndex + 2) + finalNote + '\n' + curContent.slice(sectionIndex + 1);
        } else {
            newContents = curContent.slice(0, sectionIndex) + '\n\n' + finalNote + curContent.slice(sectionIndex);
        }
    } else {
        newContents = curContent + '\n' + sectionName + '\n\n' + finalNote;
    }
    this.app.vault.modify(qcFile, newContents);
}

%>
