/* global downloadMetadata */
const html = require('choo/html');
const archiveTile = require('./archiveTile');
const modal = require('./modal');
const notFound = require('./notFound');
function password(state, emit) {
const fileInfo = state.fileInfo;
const invalid = fileInfo.password === null;
const div = html`
${state.translate('downloadFilesTitle')}
`;
if (!(div instanceof String)) {
setTimeout(() => document.getElementById('password-input').focus());
}
function inputChanged(event) {
event.stopPropagation();
event.preventDefault();
const label = document.getElementById('password-error');
const input = document.getElementById('password-input');
const btn = document.getElementById('password-btn');
label.classList.add('invisible');
input.classList.remove('border-red');
btn.classList.remove('bg-red', 'hover:bg-red', 'focus:bg-red');
}
function checkPassword(event) {
event.stopPropagation();
event.preventDefault();
const el = document.getElementById('password-input');
const password = el.value;
if (password.length > 0) {
document.getElementById('password-btn').disabled = true;
state.fileInfo.url = window.location.href;
state.fileInfo.password = password;
emit('getMetadata');
}
return false;
}
return div;
}
function createFileInfo(state) {
return {
id: state.params.id,
secretKey: state.params.key,
nonce: downloadMetadata.nonce,
requiresPassword: downloadMetadata.pwd
};
}
module.exports = function(state, emit) {
let content = '';
if (!state.fileInfo) {
state.fileInfo = createFileInfo(state);
if (!state.fileInfo.nonce) {
return notFound(state);
}
}
if (!state.transfer && !state.fileInfo.requiresPassword) {
emit('getMetadata');
}
if (state.transfer) {
switch (state.transfer.state) {
case 'downloading':
case 'decrypting':
content = html`
${state.translate('downloadingTitle')}
${archiveTile.downloading(state, emit)}
`;
break;
case 'complete':
content = html`
`;
break;
default:
content = html`
${state.translate('downloadFilesTitle')}
${state.translate('downloadMessage')}
${archiveTile.preview(state, emit)}
`;
}
} else if (state.fileInfo.requiresPassword && !state.fileInfo.password) {
content = password(state, emit);
}
return html`
${state.modal && modal(state, emit)}
`;
};