function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
import { DataTypes } from 'sequelize';
import { NOTE_RECORD_TYPE_VALUES, SYNC_DIRECTIONS, VISIBILITY_STATUSES } from '@tamanu/constants';
import { getCurrentDateTimeString } from '@tamanu/utils/dateTime';
import { Model } from './Model';
import { buildNoteLinkedJoins, buildNoteLinkedSyncFilter, getPatientIdColumnOfNotes } from '../sync/buildNoteLinkedSyncFilter';
import { dateTimeType } from '../types/model';
import { buildEncounterLinkedLookupSelect } from '../sync/buildEncounterLinkedLookupFilter';
export class Note extends Model {
    static initModel({ primaryKey, ...options }) {
        super.init({
            id: {
                ...primaryKey,
                type: DataTypes.UUID
            },
            noteTypeId: {
                type: DataTypes.STRING(255),
                allowNull: false,
                references: {
                    model: 'ReferenceData',
                    key: 'id'
                }
            },
            recordType: {
                type: DataTypes.STRING,
                allowNull: false
            },
            date: dateTimeType('date', {
                allowNull: false,
                defaultValue: getCurrentDateTimeString
            }),
            content: {
                type: DataTypes.TEXT,
                allowNull: false,
                defaultValue: ''
            },
            visibilityStatus: {
                type: DataTypes.TEXT,
                defaultValue: VISIBILITY_STATUSES.CURRENT
            }
        }, {
            ...options,
            syncDirection: SYNC_DIRECTIONS.BIDIRECTIONAL,
            validate: {
                mustHaveValidRelationType () {
                    if (!NOTE_RECORD_TYPE_VALUES.includes(this.recordType)) {
                        throw new Error(`Note: Must have a valid record type (got ${this.recordType})`);
                    }
                }
            }
        });
    }
    static initRelations(models) {
        NOTE_RECORD_TYPE_VALUES.forEach((modelName)=>{
            this.belongsTo(models[modelName], {
                foreignKey: 'recordId',
                as: `${modelName.charAt(0).toLowerCase()}${modelName.slice(1)}`,
                constraints: false
            });
        });
        this.belongsTo(models.User, {
            foreignKey: 'authorId',
            as: 'author'
        });
        this.belongsTo(models.User, {
            foreignKey: 'onBehalfOfId',
            as: 'onBehalfOf'
        });
        this.belongsTo(models.Note, {
            foreignKey: 'revisedById',
            as: 'revisedBy',
            constraints: false
        });
        this.belongsTo(models.ReferenceData, {
            foreignKey: 'noteTypeId',
            as: 'noteTypeReference'
        });
    }
    static async createForRecord(recordId, recordType, noteTypeId, content, authorId) {
        return Note.create({
            recordId,
            recordType,
            noteTypeId,
            date: getCurrentDateTimeString(),
            content,
            authorId
        });
    }
    async getParentRecord(options) {
        if (!this.recordType) {
            return null;
        }
        const parentGetter = `get${this.recordType}`;
        return this[parentGetter](options);
    }
    static async buildSyncLookupQueryDetails() {
        return {
            select: await buildEncounterLinkedLookupSelect(this, {
                patientId: getPatientIdColumnOfNotes()
            }),
            joins: buildNoteLinkedJoins().join('\n')
        };
    }
}
_define_property(Note, "buildPatientSyncFilter", buildNoteLinkedSyncFilter);

//# sourceMappingURL=Note.js.map