import { delay, getFileReaderHack, hash } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs';
import { getIbGibAddr } from '@ibgib/ts-gib/dist/helper.mjs';
import { Factory_V1 as factory, IBGIB_DELIMITER } from '@ibgib/ts-gib/dist/V1/index.mjs';
import { getGib } from '@ibgib/ts-gib/dist/V1/transforms/transform-helper.mjs';
import { GLOBAL_LOG_A_LOT } from '../../core-constants.mjs';
import { persistTransformResult, putInSpace } from '../../witness/space/space-helper.mjs';
import { getBinIb } from '../other/ibgib-helper.mjs';
import { BINARY_REL8N_NAME } from './pic-constants.mjs';
const logalot = GLOBAL_LOG_A_LOT || false;
export async function createBinIbGib({ base64Data, binHash, ext, saveInSpace, space, }) {
    const lc = `[${createBinIbGib.name}]`;
    try {
        if (logalot) {
            console.log(`${lc} starting...`);
        }
        if (!binHash) {
            throw new Error(`binHash required (E: 09da0190a9353089a9ed8a641fe0bc22)`);
        }
        const binIb = getBinIb({ binHash, binExt: ext, binEncoding: 'base64' });
        const binIbGib = { ib: binIb, data: base64Data };
        const binGib = await getGib({ ibGib: binIbGib, hasTjp: false });
        binIbGib.gib = binGib;
        if (saveInSpace) {
            // prepare
            if (!space) {
                throw new Error(`space required if saveInSpace is truthy. (E: b8a5ebbac47d757afce940d2f0af3122)`);
            }
            if (logalot) {
                console.log(`${lc} saving binary ibgib in space... (I: 8235addb1b3e4e638ef568da5f219d29)`);
            }
            // execute put
            const resSaveBin = await putInSpace({ ibGib: binIbGib, space });
            // if errored, throw
            if (!resSaveBin.success) {
                throw new Error(resSaveBin.errorMsg || 'error saving pic (E: cf892bc62ab44ec58534d9881c9c4332)');
            }
            // cleanup
            if (logalot) {
                console.log(`${lc} saving binary ibgib in space complete. (I: 4aa8d03088ad477dbee089394b3b9902)`);
            }
        }
        return { newIbGib: binIbGib };
    }
    catch (error) {
        console.error(`${lc} ${error.message}`);
        throw error;
    }
    finally {
        if (logalot) {
            console.log(`${lc} complete.`);
        }
    }
}
/**
 * shared pic code between camera and loading image via picking a file.
 */
export async function createPicAndBinIbGibs({ imageBase64, binHash, filename, ext, saveInSpace, space, }) {
    const lc = `[${createPicAndBinIbGibs.name}]`;
    if (logalot) {
        console.log(`${lc} starting...`);
    }
    try {
        const resCreateBin = await createBinIbGib({ base64Data: imageBase64, binHash, ext, saveInSpace, space });
        const { newIbGib: binIbGib } = resCreateBin;
        const binAddr = getIbGibAddr({ ibGib: binIbGib });
        // todo: do thumbnail also
        // NOTE: This is not the same filename that is saved in the bin folder!
        // This is for when the picture is downloaded outside of the ibGib system
        // or for display purposes.
        const timestamp = (new Date).toUTCString();
        filename = filename || timestamp
            .replace(':', '-')
            .replace(':', '-')
            .replace(',', ''); // temporary eek.
        if (logalot) {
            console.log(`${lc} binHash: ${binHash}`);
        }
        if (logalot) {
            console.log(`${lc} ext: ${ext}`);
        }
        const data = ext ?
            { binHash, ext, filename, timestamp } :
            { binHash, filename, timestamp };
        const rel8ns = {
            // 'pic on': [addr], // makes it more difficult to share/sync ibgibs
            [BINARY_REL8N_NAME]: [binAddr],
        };
        // create an ibgib with the filename and ext
        const resPicIbGib = await factory.firstGen({
            parentIbGib: factory.primitive({ ib: 'pic' }),
            ib: `pic ${binHash}`,
            data,
            rel8ns,
            dna: true,
            tjp: { uuid: true, timestamp: true },
            nCounter: true,
        });
        if (saveInSpace) {
            if (!space) {
                throw new Error(`space required if saveInSpace is truthy (E: 966901041c9e166c0f5ed23114003722)`);
            }
            await persistTransformResult({ resTransform: resPicIbGib, space });
        }
        return [resPicIbGib, resCreateBin];
    }
    catch (error) {
        console.error(`${lc} ${error.message}`);
        throw error;
    }
}
/**
 * @deprecated not really deprecated but i'm indicating here that this function
 * is to be changed when i am back on the web browser side of things. i have
 * changed the internal structure of the {@link BinIbGib_V1} to use `Uint8Array`
 * objects on the node side.
 */
export async function createPicAndBinIbGibsFromInputFilePickedEvent({ event, saveInSpace, space, }) {
    const lc = `[${createPicAndBinIbGibsFromInputFilePickedEvent.name}]`;
    try {
        // validate incoming input picker result
        // thanks https://edupala.com/capacitor-camera-example/
        const target = event.target;
        if (!target) {
            throw new Error(`event.target required (E: a9ade57d4359d3b0bbc75c3fea093a22)`);
        }
        if ((target.files ?? []).length === 0) {
            throw new Error(`target.files is falsy/empty (E: f2932f1012c9e04a5f06a521a381ff22)`);
        }
        const result = [];
        // going to execute serially to maintain order of incoming target files
        // IOW not going to do a Promise.all equivalent
        for (let i = 0; i < target.files.length; i++) {
            const file = target.files[i];
            if (!file) {
                throw new Error(`file required. (E: 24a8bf4cdf17dcae04b15438bc0a4522)`);
            }
            const pattern = /image-*/;
            if (!file.type.match(pattern)) {
                throw new Error(`File format not supported. file: ${file} (E: dca558def013c1c85c83f0ac088b2122)`);
            }
            // wrap reader in promise for use with async/await
            const resSingleCreate = await new Promise((resolve, reject) => {
                // const reader = new FileReader();
                throw new Error(`not implemented since change of BinIbGib.data to be Uint8Array in node. I believe here we may want to read the file as an ArrayBuffer (see readAsArrayBuffer documentation on mdn https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsArrayBuffer) (E: bb806524120cd5e40e168ef87a3ede24)`);
                const reader = getFileReaderHack();
                reader.onload = async (_) => {
                    const lc2 = `${lc}[reader.onload]`;
                    try {
                        if (logalot) {
                            console.log(`${lc2} starting... (I: 1e948476ca86b328a12700dc57be0a22)`);
                        }
                        if (!reader.result) {
                            throw new Error(`(UNEXPECTED) reader.result falsy but onload fired? (E: fdac6638fe6e2db3de1ac277bc29dc23)`);
                        }
                        let imageBase64 = reader.result.toString().split('base64,')[1];
                        let binHash = await hash({ s: imageBase64 });
                        const filenameWithExt = file.name;
                        const filenamePieces = filenameWithExt.split('.');
                        const filename = filenamePieces.slice(0, filenamePieces.length - 1).join('.');
                        const ext = filenamePieces.slice(filenamePieces.length - 1)[0];
                        if (ext.includes(IBGIB_DELIMITER)) {
                            throw new Error(`file extension cannot contain the character ${IBGIB_DELIMITER} (E: f5bc9ef79f7efe01cd53abd49d9f6122)`);
                        }
                        await delay(32); // slight doProcesses UI thread hack - not sure how much it helps (if any)
                        const resCreate = await createPicAndBinIbGibs({
                            imageBase64, binHash, filename, ext, saveInSpace, space,
                        });
                        resolve(resCreate);
                    }
                    catch (error) {
                        console.error(`${lc2} ${error.message}`);
                        reject(error);
                    }
                    finally {
                        if (logalot) {
                            console.log(`${lc2} complete. (I: d88dcaeb874c4f049d51d58655dc2b62)`);
                        }
                    }
                };
                reader.readAsDataURL(file);
            });
            result.push(resSingleCreate);
        }
        return result;
    }
    catch (error) {
        console.error(`${lc} ${error.message}`);
        throw error;
    }
}
export function isPic({ ibGib, }) {
    const lc = `[${isPic.name}]`;
    try {
        if (logalot) {
            console.log(`${lc} starting...`);
        }
        if (!ibGib) {
            throw new Error(`ibGib required (E: 1237b2d4602a3d526f6b159cb6ad0922)`);
        }
        const { ib, data, rel8ns } = ibGib;
        // try rel8ns first
        if (!rel8ns) {
            if (logalot) {
                console.log(`${lc} rel8ns falsy, not a pic (I: 563a3e779b85b070550581e652e0ca22)`);
            }
            return false;
        }
        // descends from pic^gib
        const ancestors = rel8ns.ancestor || [];
        if (ancestors && ancestors.includes('pic^gib')) {
            return true;
        }
        // has binary qualities and ib contains 'pic'
        if (!data) {
            if (logalot) {
                console.log(`${lc} data falsy, not a pic (I: 6c05c03cdfca4085aef8f2a19df91690)`);
            }
            return false;
        }
        if (data.binHash && data.ext && data.filename && ib?.includes('pic')) {
            if (logalot) {
                console.log(`${lc} has binHash, ext, filename and ib has pic (I: f754b6b023d5379ee770038617edc722)`);
            }
            return true;
        }
        if (logalot) {
            console.log(`${lc} reached end, doesn't have pic qualities. not a pic. (I: 298c5e9a2916445df1cdd22a96f57522)`);
        }
        return false;
    }
    catch (error) {
        console.error(`${lc} ${error.message}`);
        throw error;
    }
    finally {
        if (logalot) {
            console.log(`${lc} complete.`);
        }
    }
}
//# sourceMappingURL=pic-helper.mjs.map