import ExtensionsBaseRoute from './base';
import RSVP from 'rsvp';

export default ExtensionsBaseRoute.extend({
  model(params) {
    this._super(...arguments);
    let userData = this.get('session.userData');
    let areExtensionsEnabled = RSVP.resolve(userData.is_staff);

    let extension = this.getExtension(params.extensionId).then(
      ext => ext,
      () => this.returnToExtensionDiscovery()
    );

    let isInstalled = this.get('store').query('extension-installation', {
      channelId: userData.id
    }).then(
      extensions => {
        let result = extensions.any(a => a.get('extension.id') === params.extensionId);
        if (!result) {
            this.returnToExtensionDiscovery();
        }

        return result;
      },
      () => this.returnToExtensionDiscovery()
    );

    return RSVP.hash({
      areExtensionsEnabled,
      extension,
      isInstalled
    });
  }
});
