import { CustomAppsContracts, SharedContracts } from '../../contracts';
import { SharedModels } from '../shared/shared-models';

export namespace CustomAppModels {
    export class CustomApp implements SharedModels.IBaseModel<CustomAppsContracts.ICustomAppContract> {
        public id: string;
        public name: string;
        public codename: string;
        public source_url: string;
        public config: string | null;
        public allowed_roles?: SharedContracts.ICodenameIdReferenceContract[];
        public display_mode: CustomAppsContracts.CustomAppDisplayMode
        public _raw!: CustomAppsContracts.ICustomAppContract;

        constructor(data: {
            name: string;
            id: string;
            codename: string;
            source_url: string;
            config: string | null;
            allowed_roles?: SharedContracts.ICodenameIdReferenceContract[];
            display_mode: CustomAppsContracts.CustomAppDisplayMode;
            _raw: CustomAppsContracts.ICustomAppContract;
        }) {
            this.id = data.id;
            this.name = data.name;
            this.codename = data.codename;
            this._raw = data._raw;
            this.source_url = data.source_url;
            this.config = data.config;
            this.display_mode = data.display_mode;
        }
    }

    export interface IAddCustomAppData {
        name: string;
        source_url: string;
        codename?: string;
        config?: string | null;
        allowed_roles?: SharedContracts.ICodenameIdReferenceContract[];
        display_mode?: CustomAppsContracts.CustomAppDisplayMode;
    }


    export type ModifyCustomAppPropertyName = 'name' | 'source_url' | 'config' | 'allowed_roles' | 'display_mode';

    export type ModifyCustomAppOperation =
        | {
            op: 'addInto';
            value: SharedContracts.IReferenceObjectContract[];
            property_name: Extract<ModifyCustomAppPropertyName, 'allowed_roles'>;
        }
        | (
            | {
                op: 'replace';
                value: SharedContracts.IReferenceObjectContract[];
                property_name: Extract<ModifyCustomAppPropertyName, 'allowed_roles'>;
            }
            | {
                op: 'replace';
                value: string;
                property_name: Extract<ModifyCustomAppPropertyName, 'name' | 'source_url'>;
            }
            | {
                op: 'replace';
                value: string | null;
                property_name: Extract<ModifyCustomAppPropertyName, 'config'>;
            }
            | {
                op: 'replace';
                value: CustomAppsContracts.CustomAppDisplayMode;
                property_name: Extract<ModifyCustomAppPropertyName, 'display_mode'>;
            }
        )
        | {
            op: 'remove';
            value: SharedContracts.IReferenceObjectContract[];
            property_name: Extract<ModifyCustomAppPropertyName, 'allowed_roles'>;
        };
}


